【问题标题】:ReactJS - Fetch Route with Query ParametersReactJS - 使用查询参数获取路由
【发布时间】:2018-06-25 19:22:06
【问题描述】:

我的 ReactJS 设置遇到问题,我试图在我的 url 中检索查询参数以附加到我的 fetch 函数中调用的 URL 路由。我应该使用一个包来实现这一点吗? cheerio ReactJS 相关包?有什么方法可以在不使用反应路由器或 redux 的情况下做到这一点?我正在使用node-fetch 拨打我的路线。

例如下面是'http://localhost:3000/api?start_date=2017-11-05&end_date=2017-11-21'

class BlogFeedContainer extends React.Component{
    constructor(props, context) {
        super(props, context);
        this.state = this.context.data || window.__INITIAL_STATE__ || {blogs: []};
    }

    fetchList() {
        fetch('http://localhost:3000/api')
            .then(res => {
                return res.json();
            })  
            .then(data => {
                console.log("API Fetch Data Below");
                console.log(data);
                this.setState({ blogs: data.blog, user: data.user, csrf: data.csrfToken });
            }) 
            .catch(err => {
                console.log(err);
            });
    }

    componentDidMount() {
        this.fetchList();
    }

    render() {
        return (
            <div className="container">
                <BlogFeed {...this.state} />
            </div>
        )
    }
};

【问题讨论】:

标签: reactjs node-fetch


【解决方案1】:

不需要任何第三方库。只需使用window.location 对象:

window.location.search // => gives you query strings, starting with `?`

【讨论】:

    猜你喜欢
    • 2016-11-24
    • 1970-01-01
    • 2021-06-28
    • 1970-01-01
    • 2020-01-02
    • 2018-05-18
    • 1970-01-01
    • 2012-07-26
    • 1970-01-01
    相关资源
    最近更新 更多