简体   繁体   English

REACT + REDUX:在redux状态更改mapStateToProps更新状态但视图不按照新状态呈现

[英]REACT + REDUX: on redux state change mapStateToProps updating state but view is not rendering as per new state

there is my container which is getting state via redux store . 有我的容器通过redux商店获得状态。

I am passing this state to modal box via props like this : Example: 我通过这样的道具将此状态传递给模态框:示例:

render(){
  let {team,id} =this.props.data;
    return(
    <div>
    <ModalExample isOpen={this.state.isOpen} team={team} id={id}
modalClose={this.modalClose.bind(this)} 
handleAddTeam={this.handleAddTeam.bind(this)}/>
        </div>
        )}

for the first time its working perfectly... There is team list and input field with add button inside modal box.so, when i do certain add method inside Modalbox component and update state ,there i can see state change in reduxDevTool and even state is change in mapStateToProps but Modal box team list is not updated or say modalbox props doesnt recieve new props as per state change... 它第一次完美地工作...有模式框内的添加按钮的团队列表和输入字段。所以,当我在Modalbox组件内部确定添加方法并更新状态时,我可以在reduxDevTool甚至状态中看到状态更改是mapStateToProps的变化但模态框队列表没有更新或说modalbox道具根据状态变化没有收到新道具...

even in this container 即使在这个容器中

render(){
  let {team,id} =this.props.data;
    console.log(this.props.data) **//Here state change is shown**
    console.log(team) **//Here state is not changed**
    return(
    <div>
    <ModalExample isOpen={this.state.isOpen} team={team} id={id}
modalClose={this.modalClose.bind(this)} 
handleAddTeam={this.handleAddTeam.bind(this)}/>
        </div>
        )}

plus i've tried to pass props inside ModalExample via both this way 加上我试图通过这种方式在ModalExample中传递道具

team={this.props.data} , team={team} 

but still ModalExample view is not updating.. 但仍然ModalExample视图没有更新..

Confusing : If i close and open the ModalBox or type inside input field of modal box ,then there is change in view as per our new state... But i want instant modal box view render as per our redux state change... 令人困惑 :如果我关闭并打开ModalBox或输入模态框的内部输入字段,那么根据我们的新状态,视图会发生变化...但我希望根据我们的redux状态更改即时模态框视图渲染...

I am not sure this is the best way or not. 我不确定这是不是最好的方式。 But recently I solved my problem like this... 但最近我像这样解决了我的问题......

Actually, my redux store state was like this : 实际上,我的redux商店状态是这样的

user: {
    data: [
        id: 1,
        key: 'exampleKey',
        name: 'exampleName',
        team: [
            {email: 'one', role: 'one'},
            {email: 'two', role: 'two'}
        ]
    ],
    error: null
}

so, whenever I update redux store state team array like 所以,每当我更新redux store状态团队数组时

user: {
    data: [
        id: 1,
        key: 'exampleKey',
        name: 'exampleName',
        team: [
            {email: 'one', role: 'one'},
            {email: 'two', role: 'two'},
            {email: 'three', role: 'three'}
        ]
    ],
    error:null
}

the changed redux state can be easily seen in reduxDevTool 在reduxDevTool中可以很容易地看到改变的redux状态

and in container mapStateToProps I was handling state like this, 并在容器mapStateToProps我处理这样的状态,

where console.log(state.signup.user.data) even show changes in the state but this does not call componentWillReceiveProps so, my view was not rendered... 其中console.log(state.signup.user.data)甚至显示状态的变化,但是这不会调用componentWillReceiveProps所以我的视图没有呈现......

const mapStateToProps = (state) => {
    return {
        user: state.signup.user.data,
    };
};

I finally solved this issue by defining one more state in mapStateToProps 我终于通过在mapStateToProps中定义了另一个状态来解决这个问题

const mapStateToProps = (state) => {
    return {
        user: state.signup.user.data,
        teamMember:state.signup.user.data.team,
    };
};

this triggers the componentWillReceiveProps and rendered my view instantly. 这会触发componentWillReceiveProps并立即呈现我的视图。

In Redux, state change happens with some actions and reducers. 在Redux中,状态改变发生在一些动作和减速器上。

You can find them in redux documentation here Actions and Reducers . 您可以在redux文档中找到它们动作减速器

You are fetching team from the store. 你是从商店取得团队。 If you simply update the team object, it will not trigger any state change. 如果只是更新团队对象,则不会触发任何状态更改。 It will simply change the object. 它只会改变对象。 To change the state you need some action which will carry some payload. 要更改状态,您需要一些可以承载一些有效负载的操作。

For example, "UPDATE_TEAM_NAME" action will carry newName as it's payload and a reducer(pure function) will return a new team object. 例如,“UPDATE_TEAM_NAME”操作将携带newName作为其有效负载,而reducer(纯函数)将返回新的团队对象。

In addition to that, If you are not updating team object in the container, I would suggest you get your team object in ModelExample component rather than passing it as props from container. 除此之外,如果您没有更新容器中的团队对象,我建议您在ModelExample组件中获取团队对象,而不是将其作为容器中的props传递。 It'll make things clearer. 它会让事情变得更清晰。

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

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