过去几周我一直在努力学习React和Redux。现在我遇到了一个问题,还没有找到正确的答案。
假设我有一个React页面,可以从链接中获取属性。
const id = this.props.params.id;
现在在这个页面上,我想展示一个来自STORE并带有此ID的对象。
const initialState = [
{
title: 'Goal',
author: 'admin',
id: 0
},
{
title: 'Goal vol2',
author: 'admin',
id: 1
}
]
我的问题是:从STORE查询对象的函数应该在渲染方法之前的页面文件中吗?还是应该使用action creator并将函数包含在reducers中。我注意到reducers似乎只包含对store有影响的操作,而我的操作只是查询存储。提前感谢。
_find(state, {id: ownProps.params.id})
可以正常工作。但是从_.find(state, 'id', ownProps.params.id)
中我没有得到正确的结果。 - Srinivasthis.state.filter(item => item.id == id);
。 - Patrick Mutukufilter
会返回一个数组 -- 你还需要获取第一个索引项 --filter(..)[0]
。如果支持的话,现在也可以使用Array.prototype.find
。 - Dylan