如何创建独立于数组更新的组件列表

How to create a list of components which update independed out of an array

本文关键字:更新 数组 组件 列表 独立 何创建 创建 于数组      更新时间:2023-09-26

我在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。在那里,您可以检查newPropsthis.props,并进行相等性检查。如果您关心的属性发生了更改,请让它更新,否则不要更新。

shouldComponentUpdate:规范

nextProps, nextState。它需要一个返回布尔值,所以你可以说,

return nextProps.level !== this.props.level;

这意味着只有当级别不同时,它才会更新。使用你认为合适的任何条件。