如何创建独立于数组更新的组件列表
How to create a list of components which update independed out of an array
我在Array中有一个对象列表,我将其作为初始状态加载到reducer中。当我研究该数组中的对象(将级别增加1)时,即使其余部分保持不变,整个渲染列表也会更新。有没有办法在Redux中只更新该列表中的一个组件?
减速器
const researchObj = fromJS{
researchList: [{
id: 1,
name: 'iron',
level: 0
}, {
id: 2,
name: 'defence',
level: 0
}]
})
export default function (state = researchObj, action) {
switch (action.type) {
case 'RESEARCH':
return research(state, action.researchId);
}
return state;
}
function research(state, researchId) {
const researchObjIndex = state.get('researchList').findIndex(
(research) => research.get('id') === researchId
);
let researchedItem = state.get('researchList').get(researchObjIndex);
researchedItem = researchedItem.update('level', level => level + 1);
let newState = state.update('researchList', researchList => researchList.set(researchObjIndex, researchedItem));
return newState;
}
研究列表容器
export class ResearchList extends React.Component {
render() {
this.getResearchCost = (baseCost, costIncrease, level) => {
return baseCost + (baseCost * costIncrease * level);
}
return <section className={styles.root}>
{this.props.researchList.map(item =>
<ResearchItem
key={item.get('id') }
id={item.get('id') }
name={item.get('name') }
level={item.get('level') }
baseProduction={item.get('baseProduction') }
productionIncrease={item.get('productionIncrease') }
upgradeCost={this.getResearchCost(item.get('baseCost'), item.get('costIncrease'), item.get('level')) }
doResearch={this.props.research}
/>
) }
</section>
}
}
ResearchList.propTypes = {
researchList: React.PropTypes.object.isRequired,
research: React.PropTypes.func.isRequired
}
function mapStateToProps(state) {
return {
researchList: state.game.get('researchList')
};
}
export const ResearchListContainer = connect(mapStateToProps, actions)(ResearchList);
研究项目组件
export default class ResearchItem extends React.Component {
render() {
return <div className={styles.root}>
<div className={styles.stats_container}>
<span className={styles.stats_item}>Current Level: {this.props.level}</span>
<span className={styles.stats_item}>Current Production: {(this.props.level * this.props.productionIncrease) + this.props.baseProduction}</span>
</div>
<div className={styles.research_btn_container}>
<button className={styles.research_btn} onClick={() => this.props.doResearch(this.props.id) } >{this.props.name} ({this.props.upgradeCost}) </button>
</div>
</div>
}
}
根据存储更改选择性更新组件的唯一方法是将mapStateToProps
设置为存储的适当部分。在您的情况下,容器需要访问一个数组,因此您不能进一步限制存储更新。相反,我建议在ResearchItem
组件中添加一个shouldComponentUpdate
。在那里,您可以检查newProps
与this.props
,并进行相等性检查。如果您关心的属性发生了更改,请让它更新,否则不要更新。
shouldComponentUpdate
:规范
取nextProps, nextState
。它需要一个返回布尔值,所以你可以说,
return nextProps.level !== this.props.level;
这意味着只有当级别不同时,它才会更新。使用你认为合适的任何条件。
相关文章:
- JavaScript-保存、存储和更新数组
- 使用ajax(刚刚更改的值)更新数组表
- 如何使用ajax和jquery动态更新数组表
- 使用条件for循环更新数组-Javascript
- 正在RactiveJS上更新数组
- 更新数组中的嵌入文档 - Mongodb + Node Driver
- 余烬更新数组中的对象
- 通过代码+敲除更新数组中的值
- 正在尝试获取事件之外的更新数组的值
- 更新数组后,ng重复双重渲染
- 使用angularjs中复选框的值更新数组
- 将变量推送到数组中,并在每次单击时更新数组
- 如何在Mongoose中从多个数组更新数组内部的值
- 在 ReactJS 中更新数组中对象的最佳方法是什么
- 在 Rally SDK 2 中,如何更新数组字段,例如在变更集的工件上
- 更新数组对象值
- MongoDB 用变量更新数组
- AngularJS - 使用 $http 更新数组内的嵌套对象属性
- JavaScript 使用索引更新数组元素
- 如何更新数组中的所有值?-JS/Jquery.