将 React 组件存储在 Redux reducer 中?

Storing React component in a Redux reducer?

我正在为我的应用程序创建一个通用模态 React 组件来显示各种不同的东西。我希望它足够灵活以显示普通 HTML 和交互式 React 组件。

我通过将可显示组件存储在我的 Redux 模态缩减器中来让它工作。到目前为止,我 运行 没有遇到任何问题。

以前有人用过这种方法吗?我无法在网上找到任何这样的例子,所以我不确定这是否是不好的做法。如果是这样,您是否建议采用另一种方法来处理此问题?

它可能有用,但我认为您真的不需要这样做。您不应该将整个组件保存在商店中。只保存普通的 state,它应该是可序列化的,并将它们作为 props 传递给组件。组件的 render() 函数将负责渲染。

查看redux常见问题:

Can I put functions, promises, or other non-serializable items in my store state?

It is highly recommended that you only put plain serializable objects, arrays, and primitives into your store. It's technically possible to insert non-serializable items into the store, but doing so can break the ability to persist and rehydrate the contents of a store, as well as interfere with time-travel debugging.

If you are okay with things like persistence and time-travel debugging potentially not working as intended, then you are totally welcome to put non-serializable items into your Redux store. Ultimately, it's your application, and how you implement it is up to you. As with many other things about Redux, just be sure you understand what tradeoffs are involved.

http://redux.js.org/docs/faq/OrganizingState.html#can-i-put-functions-promises-or-other-non-serializable-items-in-my-store-state

您还可以阅读此线程的讨论:https://github.com/reactjs/redux/issues/1793