简体   繁体   English

状态正在更新至组件,但道具未定义

[英]State is updating to the component but props is undefined

I just tried to integrate redux into my react-native application.CombineReducer used to combine two reducers but it gives me undefined props. 我只是试图将redux集成到我的react-native应用程序中.CombineReducer曾经组合了两个reducer,但是它给了我不确定的道具。

CounterComponent CounterComponent

 function mapStateToProps(state) {
   console.log(props)  //undefined
   console.log(state); // counterReducer: { counter: 2 }
   return {
       counter: state.counter
   };
  }

reducerIndex.js reducerIndex.js

import { combineReducers } from 'redux';
import { todoReducer } from './todoReducer';
import { counterReducer } from './counterReducer';
export default combineReducers({
  counterReducer,
  todoReducer
});

App.js App.js

const store = createStore(reducer);

export default class App extends Component {
  render() {
     return (
      <Provider store={store}>
       <CounterApp />
      </Provider>
   );
  } 
}

Yeah thats a valid undefined for the props being accessed in the mapStateToProps because the mapStateToProps is just a normal function that exposes the redux state to your component that you have attached to the store using connect . 是的,对于在mapStateToProps中访问的道具来说,这是一个有效的undefined mapStateToProps因为mapStateToProps只是一个正常的函数,该函数将redux状态公开给使用connect连接到商店的组件。 mapStateToProps doesn't know your component props but it does update them or add more to it by giving your redux state to your component as props thats why it is a good convention to write the function name as mapStateToProps ! mapStateToProps不知道您的组件mapStateToProps ,但是它通过将组件的redux状态作为props来更新它们或向其添加更多内容,这就是为什么将函数名称写为mapStateToProps一个好习惯! so when you write the following: 因此,当您编写以下内容时:

class MyContainer extends Component {
   constructor (props) {
      super (props);
   }
   ......
   .....
   render () {
    return <MyCoolComponent />
   }
};

function mapStateToProps(state) {
  const {myreducer} = state;
  return {
     data: myreducer.data 
  }
}

function mapDispatchToProps (dispatch, ownProps) {
   return {
      updateData: () => {
         dispatch(someAction());  //someAction: this will return something like this: {type: MY_UPDATE_ACTION}
      }
   }
}

export default connect(mapStateToProps, mapDispatchToProps)(MyContainer);

So in the MyContainer component, you will have the access to the data as one of its props. 因此,在MyContainer组件中,您将可以访问data作为其道具之一。 You can access data anywhere in the class using this.props.data . 您可以使用this.props.data在类中的任何位置访问数据。

On the other hand mapDispatchToProps exposes functions as props to the attached component, the functions being exposed have access to the dispatch which actually dispatches an action to the store thereby giving power to your component to mutate the redux store. 另一方面, mapDispatchToProps将函数作为道具公开给附加的组件,公开的函数有权访问实际上将操作分派给存储的dispatch ,从而使您的组件有能力改变redux存储。 So with the above explanation, updateData function can be accessed from anywhere in the MyContainer using this.props.updateData() . 因此,根据以上解释,可以使用this.props.updateData()MyContainer任何位置访问updateData函数。

Hope that helps. 希望能有所帮助。 Happy Coding :) 快乐编码:)

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

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