How to parse query string in react-router v4

前端 未结 7 980
孤独总比滥情好
孤独总比滥情好 2020-12-04 16:27

In react-router v3 I could access it with props.location.query.foo (if the current location was ?foo=bar)

In react-router-dom@4.0.0

7条回答
  •  遥遥无期
    2020-12-04 17:12

    Using third party package is overkill to simple solutions

    componentDidMount() {
            const query = new URLSearchParams(
                this.props.location.search
            );
    
            let data= {};
    
            for (let params of query.entries()) {
                data[params[0]] = +params[1];
            }
            this.setState({ urldata: data});
        }
    

    this will simply convert URL data into object.

提交回复
热议问题