gpt4 book ai didi

reactjs - 带复选框的 React/Redux 组件在单击复选框时不会更新

转载 作者:行者123 更新时间:2023-12-03 13:31:27 25 4
gpt4 key购买 nike

在我的组件中,我有复选框。当我单击它们时,我想将选定的复选框保存在 redux 状态并更新我的组件,以便选定的复选框应显示为已选中。这是我的组件:

import React, {Component} from 'react';
import {connect} from 'react-redux';
import {selectIngredient} from '../actions';
class Ingredient extends Component {
constructor(props) {
super(props)
this.isSelected = this.isSelected.bind(this);
this.handleCheckbox = this.handleCheckbox.bind(this);

}


handleCheckbox(e) {
const value=e.target.value;
this.props.selectIngredient(value);
}


isSelected(id) {
return (this.props.selectedIngredients.indexOf(id) > -1)
}

render() {
return (
<div className="container">
<div className="form-check checkbox">
<label className="form-check-label">
<input checked={this.isSelected(1)}
className="form-check-input"
onClick={this.handleCheckbox}
type="checkbox" value="1" />
1
</label>

</div>
<div className="form-check checkbox">
<label className="form-check-label">
<input checked={this.isSelected(2)}
className="form-check-input"
onClick={this.handleCheckbox}
type="checkbox" value="2" />
2
</label>

</div>
<div className="form-check checkbox">
<label className="form-check-label">
<input checked={this.isSelected(3)}
className="form-check-input"
onClick={this.handleCheckbox}
type="checkbox" value="3" />
3
</label>

</div>
</div>
);
}
}

function mapStateToProps(state, ownProps){
const {selectedIngredients} = state;
return {selectedIngredients}

}

export default connect(mapStateToProps,{selectIngredient})(Ingredient);

这是action/index.js

import {SELECT_INGREDIENT} from "../constants";

export function selectIngredient(selected){
const action = {
type: SELECT_INGREDIENT,
selected
}
return action;
}

这是 reducer :

import {SELECT_INGREDIENT} from "../constants";

export default (state={selectedIngredients:[]}, action)=>{
let stateBefore = Object.assign({},state);
switch(action.type){
case SELECT_INGREDIENT:
const {selected} = action;
stateBefore['selectedIngredients'].push(+selected);
return stateBefore

default:
return state;
}

}

当我单击复选框时,没有任何变化,它们保持为空。组件未更新。但 redux 更新了它的状态。我想如果 redux 状态正在更新,那么组件及其复选框也应该更新。这是错的吗?

请帮我弄清楚如何完成这项工作。谢谢

<小时/>

下面评论中给出的答案:

在你的 reducer 中 return { ...state, selectedIngredients: [...state.selectedIngredients, action.selected] }

最佳答案

这是一个突变:

  let stateBefore = Object.assign({},state);
...
stateBefore['selectedIngredients'].push(+selected);

You are shallowly copying the state object but you still mutate the selectedIngredients because it is copied by reference so you are still modifying the existing selectedIngredients object.

检查以下问题的第一个答案 https://github.com/reactjs/redux/issues/1769

使用对象扩展运算符:

case SELECT_INGREDIENT:
return {
...state,
selectedIngredients: [...state.selectedIngredients, action.selected],
}

关于reactjs - 带复选框的 React/Redux 组件在单击复选框时不会更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47939747/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com