简体   繁体   English

如何通过 Redux 商店访问另一个组件的 state

[英]How do I access the state of another component via Redux store

Say I have two components, App and DialogBox;假设我有两个组件,App 和 DialogBox;

so my react store also has two objects所以我的反应商店也有两个对象

const rootReducer = combineReducers({
    app: appReducer,
    dialog: dialogReducer
});

export const store = createStore(rootReducer);

Now, when I have to open the Dialog box, I call the dialogBoxActions.openDialog() And, when I close the Dialog box, I call dialogBoxActions.closeDialog() and also appActions.notifyDialogClosed() ;现在,当我必须打开对话框时,我调用dialogBoxActions.openDialog()并且,当我关闭对话框时,我调用dialogBoxActions.closeDialog()appActions.notifyDialogClosed()

This works, but is there a way to do this in more clearer way?这行得通,但是有没有办法以更清晰的方式做到这一点?

For example can I use the state.dialog from the store in App?例如,我可以使用应用商店中的 state.dialog 吗? This is what I tried in the App这是我在应用程序中尝试过的

const mapStateToProps = (state) => {
    return {
        checkCloseDialog: state.dialog.openDialog
    }
}

The ComponentWillReceiveProps does get the checkCloseDialog object, but it gets the old state. ComponentWillReceiveProps确实获得了 checkCloseDialog object,但它获得了旧的 state。 I debugged to find out that it gets triggered correctly after the reducer function of the DialogBox component but I get old data.我调试发现它在 DialogBox 组件的减速器 function 之后被正确触发,但我得到了旧数据。

Is there any elegant way of accessing each other's store or is it Redux's philosophy that components should communicate with each other via actions?是否有任何优雅的方式来访问彼此的商店,或者 Redux 的理念是组件应该通过操作相互通信?

Ah, Okay.啊好吧。 my bad.我的错。

Yes, it is possible.对的,这是可能的。 And it works.它有效。 My mistake was I was trying to use it inside ComponentWillReceiveProps() method.我的错误是我试图在ComponentWillReceiveProps()方法中使用它。 The thing is, it seems the redux store gets updated later.问题是,redux 商店似乎稍后会更新。 So, ComponentWillReceiveProps() will hold an old state.因此, ComponentWillReceiveProps()将保存旧的 state。

Hence, render() and ComponentDidUpdate() methods get the updated state.因此, render()ComponentDidUpdate()方法获取更新的 state。

The aim here is to help reduce the multiple action calls between independent components.这里的目的是帮助减少独立组件之间的多个操作调用。

For example, a Dialog should not be concerned with what the caller should do after it closes itself.例如,Dialog 不应该关心调用者在它自己关闭后应该做什么。 Instead the caller should subscribe to the state of Dialog to decide what to do.相反,调用者应该订阅 Dialog 的 state 来决定要做什么。

Hope this helps others.希望这对其他人有帮助。

暂无
暂无

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

相关问题 我如何从另一个 React 组件访问 redux 状态? - How do i access redux state from another react component? 如何在 React Redux 中访问商店 state? - How do I access store state in React Redux? React-Redux:如何访问父组件中的商店? - React-Redux: How do I access the store in Parent Component? 如何将Redux存储从一个组件传递到另一个组件? - How do I pass redux store from one component to another? 如何在同一反应组件中使用本地状态以及redux存储状态? - How do I use local state along with redux store state in the same react component? 如何将组件状态存储和订阅到redux存储? - How can I store and subscribe component state to redux store? 我如何在 react redux 中访问另一个 reducer 的状态。 reducer 和 store 之间的状态流 - How do i access state of one reducer in other in react redux. State flow between reducers and store 我想将状态从一个组件发送到另一个组件来渲染它,没有 Redux 怎么办? - I want to send the state from a component to another component to render it, how to do it without Redux? React / Redux-我是否以Redux或Component状态存储Redux项目的映射值? - React/Redux - Do I store mapped values of Redux items in Redux or Component state? React-redux 组件每次在商店中发生和未连接的 state 更改时都会重新渲染。 我该如何预防? - React-redux component rerenders every time when and unconnected state changes in the store . How do I prevent it?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM