javascript - 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')
. 我还在学习,这是我到目前为止的地方。目标是让用户在数组中键入name
和lastname
插入,list
并用于dispatch
更新list
reducer 中的 initialState 提前谢谢您的回答。
屏幕
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 );
动作.JS
export const updateList = (list) => {
return { type:"UPDATE_LIST", payload:list}
}
减速器.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,
}
试试这个。
推荐阅读
- jenkins - Jenkins 管道 - 如何根据定义的参数运行阶段
- elasticsearch - 如何在 elasticsearch DSL 查询中过滤
- css - 省略号不适用于带有 flex 的引导程序
- angular - 上次三星更新后 PWA 应用程序中的键盘覆盖(覆盖)输入字段
- angular - 表格中的条件文本着色
- python - 删除与另一列重复的具有相同列值的所有行
- android - 如何在 PowerShell 中使用 ADB 返回码(或结果)?
- postgresql - 带有 BEGIN 和块的 postgres 函数和事务
- sql-server - 从多列中查找最小和最大数据
- java - Spring Boot 2.5.3 OAuth2 - Auth-Server 和 Webservice 分开,登录后没有端点工作