简体   繁体   中英

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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