我是否在我的 ReactNative 应用程序中正确实施了 redux?
Am i implementing redux correctly in my ReactNative app?
我正在使用 ReactNative 和 Redux 实现一个移动应用程序,我正在实现的应用程序看起来像这样:
Login (screen)
|--> Search for an object (screen)
|--> Show that object and edit it (screen)
|--> Take 2 photos (each photo a screen)
|--> A last screen for make a new object and save it
以上流程显示了每个屏幕如何完成工作并传递到下一个屏幕。
我的申请状态是下一个:
{
auth: {
logged: false,
token: ''
},
somethingOfSideBar...
}
但我觉得我做事的方式不对,因为大多数屏幕都有自己的状态,例如 searchSomethingScreen 从服务器获取数据,检查它是否有效并能够传递到下一个屏幕。我觉得我没有按照 Redux 的方式做事,它应该做出改变整个应用程序状态的动作,但我觉得我不需要比我拥有的更多的状态。对我来说,全局事物是身份验证数据和侧边栏(因为它存在于整个应用程序中)。
我应该为每次屏幕变化都采取行动吗?
我应该在全局状态应用程序中添加更多信息吗?
还有一件事,我有一个 AppContainer 组件,它在连接中用于访问商店,但我正在传递部分状态和操作以及子属性,我觉得这是错误的,因为嗯
我觉得 Redux Reddit tutorial 可能对您有用。这当然是给我的。
But i feel i am doing the things in the wrong way, because most of the screens have their own state, by example searchSomethingScreen fetch data from the server, check if it is valid and enable to pass to the next screen.
在 Redux 方式中使用,API 请求及其成功完成应分别映射到一个操作。在每个操作上适当地更改应用程序状态(在您的还原函数中),绑定到您的商店的 views/screens 将重新呈现。因此,如果您要发出 API 请求:
- 创建一个
Search
容器,将状态 searchResults
映射到 props
并绑定一个 Search
组件。 (例如,参见 this container。)
- 在用户输入搜索词时触发操作
REQUEST_SEARCH
。
- AJAX 请求已触发。
- AJAX 请求成功完成。使用搜索结果触发操作
RECEIVE_SEARCH
。
SearchReducer
存储搜索结果。
- 绑定
Search
组件重新呈现搜索结果。
Should i make actions for every screen change? Should i put more information in the global state application?
一般来说,是的。但有时我使用组件状态(不分派操作)来存储组件本地的状态(例如更新文本字段)。
A one more thing, i have a AppContainer component which is used in connect to have access to the store, but i am passing parts of the state and the actions as well as children properties and i feel this is wrong as well.
一般而言,更高级别的组件应该是容器,它通过 props 将状态注入到无状态组件中。您可以拥有多个容器,它们像组件一样工作,因此您可以将一个容器嵌套在另一个容器中。我鼓励您查看文档,因为它对我非常有用。 :)
我正在使用 ReactNative 和 Redux 实现一个移动应用程序,我正在实现的应用程序看起来像这样:
Login (screen)
|--> Search for an object (screen)
|--> Show that object and edit it (screen)
|--> Take 2 photos (each photo a screen)
|--> A last screen for make a new object and save it
以上流程显示了每个屏幕如何完成工作并传递到下一个屏幕。
我的申请状态是下一个:
{
auth: {
logged: false,
token: ''
},
somethingOfSideBar...
}
但我觉得我做事的方式不对,因为大多数屏幕都有自己的状态,例如 searchSomethingScreen 从服务器获取数据,检查它是否有效并能够传递到下一个屏幕。我觉得我没有按照 Redux 的方式做事,它应该做出改变整个应用程序状态的动作,但我觉得我不需要比我拥有的更多的状态。对我来说,全局事物是身份验证数据和侧边栏(因为它存在于整个应用程序中)。
我应该为每次屏幕变化都采取行动吗? 我应该在全局状态应用程序中添加更多信息吗?
还有一件事,我有一个 AppContainer 组件,它在连接中用于访问商店,但我正在传递部分状态和操作以及子属性,我觉得这是错误的,因为嗯
我觉得 Redux Reddit tutorial 可能对您有用。这当然是给我的。
But i feel i am doing the things in the wrong way, because most of the screens have their own state, by example searchSomethingScreen fetch data from the server, check if it is valid and enable to pass to the next screen.
在 Redux 方式中使用,API 请求及其成功完成应分别映射到一个操作。在每个操作上适当地更改应用程序状态(在您的还原函数中),绑定到您的商店的 views/screens 将重新呈现。因此,如果您要发出 API 请求:
- 创建一个
Search
容器,将状态searchResults
映射到props
并绑定一个Search
组件。 (例如,参见 this container。) - 在用户输入搜索词时触发操作
REQUEST_SEARCH
。 - AJAX 请求已触发。
- AJAX 请求成功完成。使用搜索结果触发操作
RECEIVE_SEARCH
。 SearchReducer
存储搜索结果。- 绑定
Search
组件重新呈现搜索结果。
Should i make actions for every screen change? Should i put more information in the global state application?
一般来说,是的。但有时我使用组件状态(不分派操作)来存储组件本地的状态(例如更新文本字段)。
A one more thing, i have a AppContainer component which is used in connect to have access to the store, but i am passing parts of the state and the actions as well as children properties and i feel this is wrong as well.
一般而言,更高级别的组件应该是容器,它通过 props 将状态注入到无状态组件中。您可以拥有多个容器,它们像组件一样工作,因此您可以将一个容器嵌套在另一个容器中。我鼓励您查看文档,因为它对我非常有用。 :)