简体   繁体   English

React:在不同路线之间导航时将数据保存在路线中

[英]React: Saving data in route when navigating between different routes

I try to save data in my route which is persisted when I am navigating between different routes.我尝试将数据保存在我的路线中,当我在不同路线之间导航时,这些数据会保留下来。 But the data is lost.但是数据丢失了。

const history = useHistory();

const handleSubmit = () => {
    history.push({pathname: '/', state: {
        nsin: props.nsin,
        assetClass: props.assetClass,
        bondsSubclass: props.bondsSubclass,
        expCurrency: props.expCurrency,
        expCountry: props.expCountry,
        instrCategory: props.instrCategory,
        nominal: props.nominal,
        openEnd: props.openEnd,
        callableByIssuer: props.callableByIssuer,
        callableByClient: props.callableByClient
    } });

}

return (
    <button onClick={handleSubmit}>
    Overview
    </button>
);

And then I try to access the state like:然后我尝试访问状态,如:

value={this.props.location?.state?.nsin}

You can create a Context to have a global state.您可以创建一个具有全局状态的上下文。 Create Context创建上下文

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM