- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我是 React Native 的新手。我正在为我的 react native 应用程序使用 redux 架构,但是由于 redux 的全局存储状态,我遇到了问题。
返回导航时,
根据 redux 架构,它正在将导航堆栈中存在的每个页面实例的状态更改为商店中的最新状态。
这里是上面例子的代码,
Page.js [组件]
class Page extends Component{
componentWillMount(){
}
render(){
var {image,apiCall} = this.props;
return (
<View>
<Image Source={{uri:image}} style={{// style for the image}}>
</View>
)}
componentDidMount(){
this.props.apiCall(this.props.route.data.link); // this.props.route.data.link -> this is just a link for // the apiCall
}
componentWillReceiveProps(){
}
shouldComponentUpdate(nextProps, nextState){
if(this.props.image==nextProps.image){
return false;
}
return true;
}
componentWillUpdate(){
}
componentDidUpdate(){
}
componentWillUnmount(){
}
}
const mapStateToProps = (state) => {
return {
image: state.PageDataReducer.image
};
};
const mapDispatchToProps = (dispatch) => {
return {
apiCall: (link) => {
dispatch(fetchProduct(link)); // fetchProduct() -> is a function which fetches the product from the // network and dispatches the action.
},
};
};
export default connect(mapStateToProps,mapDispatchToProps)(Page);
fetchProduct() [获取产品的函数]
export function fetchProduct(link) {
return function(dispatch){
// fetches the product over network
dispatch(fetchedProduct(productLink));
}
}
fetchedProduct [操作]
export const fetchedProduct = (productLink) => {
return {
type: FETCHED_PRODUCT,
image: image
};
};
PageDataReducer [缩减器]
export default function PageDataReducer(state = initialState, action) {
switch (action.type) {
case FETCHED_PRODUCT:
var newState = {
...state,
image: action.image
};
return newState;
default:
return state;
}
}
我的观察:每当导航中出现相同的页面并且该页面存储中的状态发生变化时,它就会调用该页面的 mapStateToProps() 次数在导航堆栈中。因此,它循环遍历该页面的生命周期方法以根据最新状态更改状态的次数。
在此示例中,当我点击抽屉中的香蕉时,它将商店中的状态从芒果更改为香蕉并且 mapStateToProps() 被调用了 3 次(因为该页面在导航堆栈中出现了 3 次) 因此 从 componentWillReceiveProps() 到 componentDidUpdate() 的所有生命周期方法都被调用了 3 次只是为了改变根据最新状态显示该页面的状态。
我想要的:我想要导航中页面的不同状态,以便在返回时我可以看到我访问过的所有不同产品。
根据 redux 架构,问题很明显,但我没有找到解决它的诀窍。非常感谢任何帮助或任何其他解决方法来实现我的目标。
最佳答案
您应该将域数据(例如有关苹果、芒果和香蕉的数据)的存储与 UI 状态(例如当前选择的项目)的存储分离。在域数据部分,您将存储所有已加载的项目(您可能希望稍后添加一些垃圾收集策略,例如最近最少使用的列表以节省一些内存)。
然后, View 组件将连接到应用状态树的两个部分:连接到 UI 部分以获取当前选择的内容,连接到域部分以获取有关所选项目的详细信息。
在您的 mapStateToProps 函数中,首先提取当前选择,这必须是一个标识符。您也可以从其他地方提取它,例如来自组件 Prop (mapStateToProps 的第二个参数)。然后使用提取的标识符从域数据存储中获取已加载的数据。如果选择的数据不在商店中(例如,当用户第一次访问该页面时,或者它已经被垃圾收集),您将空值放入 Prop ,这意味着 componentDidMount 或 componentWillReceiveProps 钩子(Hook)他们必须发出数据加载请求(它可能被实现为向 saga 或 thunk 发出数据需求信号的 Action 的分派(dispatch),具体取决于您用于管理数据加载的内容),当发生这种情况时,渲染将返回一些“正在加载...” stub 。当请求完成时,另一个调度将更新域数据存储更新,这将触发连接进行另一个 mapStateToProps 调用,这将导致发现所选数据已经在存储中,因此不会发出新的数据加载请求,渲染器将有足够的数据用于显示。
关于android - react native -Redux : Multiple instances of same state in app,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40023091/
我知道我的问题有点含糊,但我不知道如何描述它。我问过很多地方,但似乎没有人理解我为什么要这样做。但请耐心等待,我会解释为什么我想要这样的东西。 我使用 Liquid Templates 允许用户在我的
这个问题在这里已经有了答案: what is the difference between null != object and object!=null [duplicate] (2 个回答) 7年
当我在我的本地主机 Google App Engine 应用程序中将日志记录级别更改为 FINE 时,我开始在我的跟踪堆栈中看到这些: Apr 17, 2013 4:54:20 PM com.goog
Python 有内置函数 type : class type(object) With one argument, return the type of an object. The return v
我正在使用深度学习进行语义分割,我遇到了以下术语:语义分割、实例检测、对象检测 和对象分割. 它们有什么区别? 最佳答案 这些术语的某些用法对用户而言是主观的或依赖于上下文,但据我所知对这些术语的合理
我面临 -[NSConcreteMutableData release] 的问题:消息发送到已释放的实例,我也附上了我的示例代码。 - (IBAction)uploadImage { NSString
我试图显示模型中的单个实例(数据库行),其中多个实例共享多行的相同字段(列)值。为了澄清这一说法,我有以下情况: ID/Title/Slug/Modified 1 Car A 1s ag
我正在尝试使用mockito来模拟服务。然而,我没有找到一种方法来告诉mockito,给定一个类的实例返回给我相同的实例: 类似于: given(service.add(any(Individua
我知道如何从父类(super class)原型(prototype)创建子类原型(prototype)。但是,如果我已经有了父类(super class)对象的实例来创建子类对象怎么办? 在 JS 中
鉴于 Kotlin 1.1。对于某个类的 instance,instance::class.java 和 instance.javaClass 似乎几乎是等价的: val i = 0 println(
这个问题在这里已经有了答案: 8年前关闭。 Possible Duplicate: Find out the instance id from within an ec2 machine 我正在寻找从
为什么我的 Instantiate 函数没有创建 That 的“空白”实例? 我有以下最小类: classdef That < handle properties This = '' end
Session session = HibernateUtil.getSessionFactory().openSession(); Transaction tx = session.beginTra
考虑以下几点: public class A { public String name = "i am a A instance"; } public class B extends A {
我正在使用 Scalr 来扩展网站服务器。 在 Apache 服务器上,我安装了 Sakai,并为 Linux 机器创建了一个启动脚本。 问题是,如何确保MySQL实例在Apache服务器启动之前启动
Android Realm DB 允许使用 Realm.getInstance() 获取多个实例。这些中的最佳实践是什么? :1.创建单个实例(应用程序范围)并在任何地方使用它2. 在需要时获取一个新
我很难理解为什么修改实例 a 中的属性会修改实例 b 中的相同属性。 var A = function (){ }; A.prototype.data = { value : 0 }; var
我将 Weka 用作更长管道的一部分,因此,我无法承受将所有数据写入文件或数据库只是为了创建一个 Instances。目的。我可以即时做的是创建 Instance 的列表对象。 来自 this pag
class C: def func(self, a): print(a) c = C() print(c.__dict__) # {} c.func = c.func # c.func i
Angular Routing 文档提到了组件实例创建、组件实例激活和路由激活。 文档没有解释这些概念的区别,以及每次创建/激活发生的时间。 问题 实例创建和实例激活有什么区别? 实例激活和路由激活有
我是一名优秀的程序员,十分优秀!