【问题标题】:componentDidUpdate doesn't update state with ReduxcomponentDidUpdate 不使用 Redux 更新状态
【发布时间】:2021-06-18 19:21:54
【问题描述】:

这个应用程序的目标是让我们使用React-ReduxReact 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


【解决方案1】:

我认为,当您调度一个动作时,您并没有将 action.payload 添加到 state.itemList。我的意思是什么

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{
        ...state,
        itemList: addItemToList(state.itemList,action.payload), //a util function to add items to the list. 
// And action.payload is the value ,which passed when you are dispatching the action 'ADD_ITEM'
      }
      default:
        return state
  }
  
};

当您发送操作时,它应该是

componentDidMount (props, state) {
    if (this.props.route.params?.itemList) {
      () => this.props.dispatch({type:'ADD_ITEM',payload:this.props.route.params.itemList}); 
// passing the itemList to as the payload of the action.

    }
    return null;
  }

我想这个修改应该足够了。更多关于 React-Redux here

【讨论】:

  • 尝试在你的项目中使用选择器,也不要忘记编写大量的console.log进行调试。让我知道我能提供什么帮助。 @Salvo
  • 如果组件没有挂载,那么不知何故该组件没有被调用。您在哪里导入了 ListItem 类?
  • 在 createItem screen 中,当您按下按钮时,您将状态值作为参数发送到 submitItem 函数中,但状态参数最初被声明为空,因此当您导航到 ListItem 时,基本上是两个传下来的空值。我认为这就是它给你空值的原因。
  • 现在来看看为什么 ListItem 类组件没有挂载,你可以看到 React 遵循单向流,我的意思是,当子组件被挂载时,父组件也被挂载,但是说您更改了子组件中的状态,父组件不会再次挂载,因为它遵循单向流。所以你必须仔细设计你的应用,哪个组件是子组件,哪个组件是父组件。
  • 谢谢,是的,我通常是编程新手,所以我还在学习,有没有其他方法可以在新状态出现时更新组件?
猜你喜欢
  • 2019-08-29
  • 1970-01-01
  • 2019-01-10
  • 1970-01-01
  • 1970-01-01
  • 2020-08-01
  • 2021-10-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多