【问题标题】:How to pass variable in URL React js如何在 URL React js 中传递变量
【发布时间】:2021-11-23 17:54:58
【问题描述】:

在 react js 中,我制作了一个简单的日期选择器,并从下拉日历中选择日期并将其显示在控制台中。

存储在变量中的日期,现在如何在 URL 中使用该变量

我的问题:

  1. 如何在 React js URL 中传递参数

  2. 如何在控制台日志中打印参数

代码:

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

日期来自日期选择器表单,所以传递日期动态所有如何做到这一点

【问题讨论】:

    标签: javascript reactjs api axios fastapi


    【解决方案1】:

    您可以使用template literals 传递动态值,如下所示。

    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)
    
    
        })
    }
    

    【讨论】:

      【解决方案2】:
       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);
      

      【讨论】:

      • 虽然此代码可以解决问题,including an explanation 说明如何以及为什么解决问题将真正有助于提高您的帖子质量,并可能导致更多的赞成票。请记住,您正在为将来的读者回答问题,而不仅仅是现在提出问题的人。请edit您的答案添加解释并说明适用的限制和假设。
      猜你喜欢
      • 2018-10-29
      • 2020-07-24
      • 1970-01-01
      • 1970-01-01
      • 2020-10-14
      • 1970-01-01
      • 1970-01-01
      • 2018-05-08
      • 1970-01-01
      相关资源
      最近更新 更多