【发布时间】:2021-06-18 19:21:54
【问题描述】:
这个应用程序的目标是让我们使用React-Redux 和React Navigation 在列表项目屏幕中的Flatlist 中添加一个项目。基本上,我在 Create Item 屏幕中输入名称和类别,然后使用 React Navigation 以数组的形式将其发送到 List Item 屏幕,一旦进入 List Item 屏幕,我就使用 componentDidMount 来调度操作和更新类组件中的状态,问题是什么都没有显示,即使使用 console.log 它只会让我返回 Redux Reducers 屏幕中存在的空数组。
创建项目屏幕
export class item extends Component {
constructor(props) {
super(props);
this.state = {
name: '',
category: '',
};
}
submitItem = (name, category) => {
this.props.navigation.navigate("ListItem", {
itemList: {
name: name,
category: category,
}});
};
render() {
const { name, category } = this.state;
return (
<Container>
<Header>
<Left>
<Button onPress={() =>
this.submitItem(
this.state.name,
this.state.category,
)
}>
<Text>Sumbit</Text>
</Button>
项目列表屏幕
class ListItem extends Component {
constructor(props) {
super(props);
this.state = {
itemList:[],
};
}
componentDidMount (props, state) {
if (this.props.route.params?.itemList) {
() => this.props.dispatch({type:'ADD_ITEM'});
}
return null;
}
REDUX 减速器
const initialState = {
currentUser: null,
itemList: [],
};
export const user = (state = initialState, action) => {
switch (action.type){
case USER_STATE_CHANGE:
return {
...state,
currentUser: action.currentUser,
};
case 'ADD_ITEM':
return{
itemList: state.itemList,
}
default:
return state
}
};
【问题讨论】:
-
componentDidUpdate不会为初始渲染调用。听起来您需要在componentDidMount或两者中处理它 -
你是对的,我用那个切换它,而不是它返回错误:
undefined is not an object (evaluating 'props.route'),请原谅我,但我还在学习编码 -
你可能需要使用
this.props -
谢谢,它不再返回错误,但现在它什么都不返回了
标签: javascript reactjs react-native redux react-redux