[英]Passing state props to components in route
I have the following react structure 我有以下反应结构
<Provider store={ store }>
<Router>
<Switch>
<Route path="/how-to" component={ Help } />
<Route path="/start" component={ StartPage } />
<Route path="/game" component={ GamePlay } />
<Route exact path="/" component={ Home } />
</Switch>
</Router>
</Provider>
There is a child in the StartPage
component that interacts with a property in the store state StartPage
组件中有一个孩子,该孩子与处于商店状态的属性进行交互
.....
// pickBottle is an action from the store for the state
<div className="text-center">
<button alt={bottleData.name} onClick={ (e) => { self.props.pickBottle(bottleData) } }>{bottleData.name}</button>
</div>
...
export default connect(state => ({ bottle: state }), { pickBottle } )(PickerSlides)
The component works as expected as the this.props.bottle is set on the click event. 由于在click事件上设置了this.props.bottle,因此该组件可以正常工作。 However,
然而,
When I navigate to the GamePlay
component which has 当我浏览到该
GamePlay
具有分量
.... ....
render() {
const { store } = this.context;
console.log(store);
return (
<div id="game" className="panel" >
<section className="row flexbox">
<header>
<hgroup>
<h2 id="tries" className="tries">Tries: <span>{ this.state.tries }</span></h2>
<h3 className="bottles">Opened:<span id="score" className="opened">{ this.state.bottlesOpened }</span></h3>
</hgroup>
</header>
<article className="row flexbox">
</article>
</section>
</div>
);
}
}
// map state to props
const mapStateToProps = ( state ) => {
console.log('map state ', state );
return {
bottle: state,
}
}
export default connect(mapStateToProps)(GamePlay)
console.log('map state', state )
is undefined console.log('map state', state )
未定义
Does redux work with components across pages? redux是否可以跨页面使用组件? Do I need to use a localStorage to help redux persist through routes?
我是否需要使用localStorage来帮助Redux通过路由持久化?
How can I pass the state as an updated property of the component via the Route/ Switch components? 如何通过路由/交换组件将状态作为组件的更新属性传递?
You are probably reloading the page. 您可能正在重新加载页面。 Redux's state is stored in memory, and so when you refresh the page it is lost;
Redux的状态存储在内存中,因此刷新页面时,它会丢失; see this .
看到这个 。 You can use something like redux-persist to store your app's state in local storage, etc.
您可以使用redux-persist之类的东西将应用程序的状态存储在本地存储中,等等。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.