• Jobs
  • About Us
  • professionals
    • Home
    • Jobs
    • Courses and challenges
  • business
    • Home
    • Post vacancy
    • Our process
    • Pricing
    • Assessments
    • Payroll
    • Blog
    • Sales
    • Salary Calculator

0

236
Views
How to pass variable in URL React js

in react js I made a simple date picker and select date from the dropdown calendar and I displayed it in the console.

that date stored in a variable, now how to use that variable in URL

My questions :

  1. How to pass parameter in React js URL

  2. how to print parameter in console log

code:

import React, { Component } from 'react'
import axios from 'axios'
class PostForm extends Component {
    constructor(props) {
        super(props)

        this.state = {
            key: '',
            
        }
    }

    changeHandler = e => {
        this.setState({ [e.target.name]: e.target.value })
    }

    submitHandler = e => {
        e.preventDefault()
        console.log(this.state)
        axios
            .get('http://127.0.0.1:8000/hvals_hash?key=31/8/21')
            .then(response => {
                console.log(response)
            })
            .catch(error => {
                console.log(error)
            })
    }

    render() {
        const { key } = this.state
        return (
            <div>
                <form onSubmit={this.submitHandler}>
                    <div>
                        <input
                            type="text"
                            name="key"
                            value={key}
                            onChange={this.changeHandler}
                        />
                    </div>

                    <button type="submit">Submit</button>
                </form>
            </div>
        )
    }
}

export default PostForm

A date is from date picker form , so pass date dynamicall how to do that

about 3 years ago · Juan Pablo Isaza
2 answers
Answer question

0

You can use template literals to pass dynamic values as follows.

componentDidMount(){
    const date = "20/8/21";
    axios.get(`http://127.0.0.1:8000/hvals_hash?key=${date}`)
    .then(response => {
        this.setState({
            posts:response.data
        })
        console.log(response.data)


    })
}
about 3 years ago · Juan Pablo Isaza Report

0

 import {
  BrowserRouter,
  useParams,
  Route,
  Routes,
  Link,
} from 'react-router-dom';

page:send.jsx  =>

  <Link to="/get" state={{ from: "test" }}>
        test
        </Link>
page:get.jsx

import {
  BrowserRouter,
  useParams,
  Route,
  Routes,
  Link,
  useNavigate,
  useLocation,
} from "react-router-dom";

  const location = useLocation();
const { from } = location.state ? location.state : "null";
  console.log(from);

send:
  const { navigate } = useNavigate();
  navigate("/get", { state: { from: "test" } });
get:
  const location = useLocation();
  const { from } = location.state;
  console.log(from);
about 3 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Our process Sales
Legal
Terms and conditions Privacy policy
© 2025 PeakU Inc. All Rights Reserved.

Andres GPT

Recommend me some offers
I have an error