Redux是否会自动更新与其连接的React组件?

给我的印象是,当我的Redux商店得到更新(通过将操作分派给reducer)时,我的Provider应该使新商店对其所有子组件可用。因此,当我使用mapstatetoProps()将容器组件连接到商店时,当该组件收到随商店更新的新道具时,该组件应重新渲染,而无需componentWillReceiveProps()。我认为这是错误的吗?

在阅读了几个小时的文档和其他堆栈溢出答案之后,有些东西对我来说并不是单击。我很确定我的减速器可以正常工作并返回一个新对象,我的组件可以访问存储,并且我的初始状态很好。如果有人能让我对事情的流程有更好的了解,我将永远感激不已。

基本上,我有一个标头组件,用于导入商店,使用Provider并呈现“ FAQ”组件:

import React from 'react'; 
import FAQ from './Faq';
import {Provider} from "react-redux";
import  store  from '../store/index'

class Header extends React.Component {
    render() {
        return (
                <Provider store = {store}>
                    <FAQ  />
                </Provider>
        )
    }
}
export default Header;

“ FAQ”组件是一个通过mapstatetoProps()连接到商店的容器,它导入了“ FAQTest”组件,这是一个演示组件,它将作为道具传递this.state.thisFood,以便它可以呈现存储中的数据。还有一个“ addFood”函数可以调度我的操作,该函数可以在底部的mapDispatchToProps()中看到。

import React from 'react'; 
import {connect} from 'react-redux';
import FAQTest from './faqTest';

class FAQ extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            thisFood: props.breakfast
        };
    }

    //adding this makes the component state update,but I think it should work without it
    componentWillReceiveProps(nextProps){
        this.setState({thisFood: nextProps.breakfast})
    }

    addFood = () => {
        this.props.addFood();
    }

    render() {
                return (

                    <React.Fragment>
                        <button onClick={this.addFood}> Add Food </button> 
                        <FAQTest food = {this.state.thisFood} />
                        </React.Fragment>
                    )
            }
}



const mapstatetoProps = function(state) {
    return {
        breakfast: state.faq.breakfast
    }
}

function mapDispatchToProps(dispatch) {
    return {
        addFood: () => dispatch({type: 'ADD_FOOD',food: 'Waffles'})
    }
}
export default connect(mapstatetoProps,mapDispatchToProps)(FAQ);

当我单击“添加食物”按钮时,由于mapstatetoProps(),我的商店得到了更新,而我的FAQ容器组件的道具也得到了更新。我以为这会触发我的FAQ组件更新其状态,但是除非使用componentWillReceiveProps,否则不会更新该状态。这按预期工作吗?

以防万一我做傻事,这是我的减速器:

const initialState = {
    breakfast: ["eggs","bacon"]
  }

export default function faqReducer(state = initialState,action) {

  switch (action.type) {
    case "ADD_FOOD":
      return Object.assign({},state,{
        breakfast: [...state.breakfast,action.food]
      })
    default:
      return state;
  }
}

这是我的带有我的CombineReducers()函数的根减速器:

import { combineReducers } from "redux";
import faq from './faqReducer'

export default combineReducers({
  faq: faq
});
slayerwlt1 回答:Redux是否会自动更新与其连接的React组件?

问题在于,您是将数据从道具复制到状态,但仅在安装组件时才这样做,然后期望状态在新道具到达时以某种方式更新。

Copying data from props to state is almost always the wrong approach。请不要那样做。只需使用道具中的值即可。

另外两个改进代码的建议:

  • 更喜欢使用the "object shorthand" form of mapDispatch,而不是将其编写为函数
  • 我们建议使用新的官方Redux Starter Kit package作为编写Redux逻辑的标准方法。它包含一些实用程序,可简化几种常见的Redux用例,包括商店设置,定义化简器,不可变的更新逻辑,甚至一次创建整个状态的“切片”。
本文链接:https://www.f2er.com/3120954.html

大家都在问