![](/img/trans.png)
[英]Redux, react. How I can add mapStateToProps into this.state?
[英]React: mapStateToProps - How do I get state?
I use mapStateToProps, mapDispatchToProps to pass function and state to myComponent, I can get function but can't get state. 我的代碼如下所示:
const mapStateToProps = (state) => ({
...state.editor
});
const mapDispatchToProps = (dispatch) => ({
onLoad: (payload) => dispatch(actionCreators.myFunc(payload)),}
});
function myComponent({errors, match, onLoad}) {
//function myComponent({errors, match, onLoad, state}) { //state undefined
//function myComponent({errors, match, onLoad}, state) { //state undefined
// How do I get state here?
console.log(state) //always undefined
console.log(state.editor) //undefined
console.log(editor) //undefined
}
export default connect(mapStateToProps, mapDispatchToProps)(myComponent);
如下更改您的mapStateToProps
const mapStateToProps = (state) => ({
editor:state.editor
});
Functional component
的典型示例是
function YourComponent({ yourProp }) {
return <p>{yourProp}</p>
}
function mapStateToProps(state) {
return { yourProp: state.yourProp };
}
export default connect(mapStateToProps)(YourComponent);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.