Javascript 状态正在更新到组件,但道具未定义
我刚刚尝试将redux集成到我的react原生应用程序中。CombineReducer用于组合两个还原程序,但它给了我未定义的支持 反组件Javascript 状态正在更新到组件,但道具未定义,javascript,react-native,redux,react-redux,Javascript,React Native,Redux,React Redux,我刚刚尝试将redux集成到我的react原生应用程序中。CombineReducer用于组合两个还原程序,但它给了我未定义的支持 反组件 function mapStateToProps(state) { console.log(props) //undefined console.log(state); // counterReducer: { counter: 2 } return { counter: state.counter }; }
function mapStateToProps(state) {
console.log(props) //undefined
console.log(state); // counterReducer: { counter: 2 }
return {
counter: state.counter
};
}
reducerIndex.js
import { combineReducers } from 'redux';
import { todoReducer } from './todoReducer';
import { counterReducer } from './counterReducer';
export default combineReducers({
counterReducer,
todoReducer
});
const store = createStore(reducer);
export default class App extends Component {
render() {
return (
<Provider store={store}>
<CounterApp />
</Provider>
);
}
}
App.js
import { combineReducers } from 'redux';
import { todoReducer } from './todoReducer';
import { counterReducer } from './counterReducer';
export default combineReducers({
counterReducer,
todoReducer
});
const store = createStore(reducer);
export default class App extends Component {
render() {
return (
<Provider store={store}>
<CounterApp />
</Provider>
);
}
}
conststore=createStore(reducer);
导出默认类应用程序扩展组件{
render(){
返回(
);
}
}
是的,对于在MapStateTrops
中访问的道具,这是一个有效的未定义的,因为MapStateTrops
只是一个普通函数,它将redux状态暴露给您使用connect
连接到存储的组件MapStateTrops
不知道您的组件道具,但它会更新它们,或者通过将组件的redux状态作为道具来添加更多道具,这就是为什么将函数名写为MapStateTrops
是一个好习惯!
因此,当您编写以下内容时:
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);
类MyContainer扩展组件{
建造师(道具){
超级(道具);
}
......
.....
渲染(){
返回
}
};
函数MapStateTops(状态){
const{myreducer}=状态;
返回{
数据:myreducer.data
}
}
功能图DispatchToprops(分派,ownProps){
返回{
更新数据:()=>{
dispatch(someAction());//someAction:这将返回如下内容:{type:MY_UPDATE_ACTION}
}
}
}
导出默认连接(MapStateTrops、mapDispatchToProps)(MyContainer);
因此,在MyContainer
组件中,您可以访问数据作为其道具之一。您可以使用this.props.data
访问类中任何位置的数据
另一方面,mapDispatchToProps
将函数作为道具公开给连接的组件,公开的函数可以访问dispatch
,它实际向存储区发送一个操作,从而使组件能够改变redux存储区。因此,根据上述说明,可以使用this.props.updateData()
从MyContainer
中的任何位置访问updateData
函数
希望有帮助。快乐编码:)是的,对于在MapStateTrops
中访问的道具,这是一个有效的未定义的,因为MapStateTrops
只是一个普通函数,它将redux状态暴露给您使用connect
连接到存储的组件MapStateTrops
不知道您的组件道具,但它会更新它们,或者通过将组件的redux状态作为道具来添加更多道具,这就是为什么将函数名写为MapStateTrops
是一个好习惯!
因此,当您编写以下内容时:
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);
类MyContainer扩展组件{
建造师(道具){
超级(道具);
}
......
.....
渲染(){
返回
}
};
函数MapStateTops(状态){
const{myreducer}=状态;
返回{
数据:myreducer.data
}
}
功能图DispatchToprops(分派,ownProps){
返回{
更新数据:()=>{
dispatch(someAction());//someAction:这将返回如下内容:{type:MY_UPDATE_ACTION}
}
}
}
导出默认连接(MapStateTrops、mapDispatchToProps)(MyContainer);
因此,在MyContainer
组件中,您可以访问数据作为其道具之一。您可以使用this.props.data
访问类中任何位置的数据
另一方面,mapDispatchToProps
将函数作为道具公开给连接的组件,公开的函数可以访问dispatch
,它实际向存储区发送一个操作,从而使组件能够改变redux存储区。因此,根据上述说明,可以使用this.props.updateData()
从MyContainer
中的任何位置访问updateData
函数
希望有帮助。快乐编码:)是的,它应该给你未定义的,因为mapStateToProps的内部不知道什么是道具。仅在函数内部传递状态。你仍然可以在你的类中使用道具并获取redux存储值。是的,它应该给你未定义的值,因为mapStateToProps的内部不知道道具的含义。仅在函数内部传递状态。您仍然可以在类中使用道具并获取redux存储值。