React Redux 不更新 initialState



我一直试图在我的React Native应用程序中应用Redux,但initialState没有更新,当我运行代码时,它会返回mapStateToProps() in Connect(List) must return a plain object. Instead received undefined.undefined is not an object (evaluating '_this3.state')。我还在学习,这就是我迄今为止的成就。目标是让用户键入namelastname并插入到数组list中,并使用dispatch更新reducer中的initialStatelist提前感谢您的回答。

屏幕

import { connect } from 'react-redux'
import { updateList } from './action'
export class List extends Component {
constructor(props) {
super(props);
this.state = {
list: this.props.list,
name: '',
lastname: '',

};
}

addItem = ( name, lastname ) => {   

const  item = {
key: Math.random(),
name: name,
lastname: lastname,
};
this.setState({ list: [...this.state.list, item]})
};
submitItem = ( list ) => {
() => this.props.dispatch(updateList(list));
this.props.navigation.navigate('list');
}
render(){
return(

<Button onPress={() => {this.addItem(this.state.name, this.state.lastname); this.submitItem(this.state.list)}}>
<Text>Press Submit</Text>
</Button>
)}
mapStateToProps = (state) => {
return this.state
}
mapDispatchToProps = {
updateList
}
export default connect(mapStateToProps, mapDispatchToProps)(List );

ACTION.JS

export const updateList = (list) => {
return { type:"UPDATE_LIST", payload:list}
}

REDUCER.JS

const initialState = {
currentUser: null,
list: [],
};

export const user = (state = initialState, action) => {
switch (action.type){
case USER_STATE_CHANGE:
return {
...state,
currentUser: action.currentUser,
};
case 'UPDATE_LIST': 
return{
...state,
list: [...action.payload],
}
default:
return state
}

};
case 'UPDATE_LIST':
return {
...state,
list: action.payload,
}

试试这个。

最新更新