简体   繁体   English

Redux状态/子组件渲染触发器在反应吗?

[英]Redux state / subcomponent render triggers in react?

I have a react component called contacts and subcomponents called 'comments' and 'card'. 我有一个称为contacts的反应组件,以及一个名为“评论”和“卡片”的子组件。 All three are connected to the contactReducer 全部三个都连接到contactReducer

If I wrote a reducer for contacts as follows: 如果我为联系人编写了一个reducer,如下所示:

const reducer = ( state = null, action = null ) => {
    switch (action.type) {
        case GET_CONTACT:
            return [
                ...state, 
                contact: action.payload
            ]
        case GET_CARD:
            return [
                ...state, 
                card: action.payload
            ]
        case GET_COMMENTS:
            return [
                ...state, 
                comments: [...action.payload]
            ]
        default:
            return state;
    }
}
export default reducer;

1.a) Does the parent component render each time the subcomponent receives updated props because they are all connected to the contactReducer ? 1.a)上级组件是否在子组件每次都收到更新的道具时渲染,因为它们都已连接到contactReducer

1.b) If I'm going about this the wrong way how would you recommend it be handled? 1.b)如果我使用的方式错误,您将如何处理它?

2) after each action has update the contactReducer once do I end up with the following object or am I way off? 2)在每个动作更新了contactReducer之后,我是否最终会contactReducer以下对象或离开?

contactsReducer = {
    contact : {someData},
    card    : {someData},
    comments: [someData, someData, ...]
}

Any object that receives a new object as a prop or if their state changes will re-render in accordance with react's diffing algorithm. 接受新对象作为道具或状态改变的任何对象将根据react的差异算法重新渲染。 That is assuming you don't overwrite the componentShouldUpdate method. 那是假设您不覆盖componentShouldUpdate方法。

1a - the parent will receive the new state from the reducer and re-render anything that should change. 1a-父级将从减速器接收新状态并重新渲染应更改的任何内容。

1b / 2 - In your current implementation, you will have a single array with a mix of objects with keys varying from contact, card, and comments . 1b / 2-在当前的实现中,您将拥有一个数组,其中包含对象的组合,其键从contact, card, and comments You should look into redux's functionality combineReducers and create a reducer for each of those keys. 您应该研究redux的功能combineReducers并为每个键创建一个reducer。 I would strongly recommend you watch a video series on Redux to see the design patterns, it will go a long way - video 我强烈建议您在Redux上观看视频系列以了解设计模式,这将有很大帮助- 视频

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM