当我的复选框被选中时,我正在使用redux将对象连接到数组的位置。但是在一种特定情况下,它将同一对象在我的数组中连接两次。
实际上我有这种情况:
index.js
renderCheckbox = ({ input, label, campoProducao }) => {
if (input.value && !this.props.isSubmitRecomendadoSuccessful) {
this.props.thunks.campoProducaoWasChecked([campoProducao]);
} else if (
input.value === false &&
!this.props.isSubmitRecomendadoSuccessful
) {
this.props.thunks.campoProducaoWasUnchecked([campoProducao]);
}
return (
<Checkbox
checked={input.value ? true : false}
onCheck={input.onChange}
onBlur={this.props.isSubmitRecomendadoSuccessful}
iconStyle={{ fill: 'white' }}
/>
);
};
actions.js
export const campoProducaoChecked = listaCampoProducao => ({
type: TYPES.CAMPO_PRODUCAO_CHECKED,
payload: listaCampoProducao,
});
export const campoProducaoUnchecked = campoProducao => ({
type: TYPES.CAMPO_PRODUCAO_UNCHECKED,
payload: campoProducao,
});
reducer.js
case TYPES.CAMPO_PRODUCAO_CHECKED:
return Object.assign({}, state, {
camposProducaoChecked:
state.camposProducaoChecked.concat(payload),
});
case TYPES.CAMPO_PRODUCAO_UNCHECKED:
return Object.assign({}, state, {
camposProducaoChecked: state.camposProducaoChecked.filter(
checked => checked.id !== payload[0].id,
),
});