-6ren">
gpt4 book ai didi

javascript - 数据重复并且输出显示两次。可能存在传播错误

转载 作者:行者123 更新时间:2023-12-03 00:25:43 26 4
gpt4 key购买 nike

I only enter the values one time and its gets duplicated[1]这里的输出重复了两次,我知道它是返回格式中的内容。如何解决这个问题?

 export const addToDo = (state, action) => {
console.log(">>>>>>>>>>>>>>>>>>>>>>>>>>");
const { name, email, phone } = action;
const tempTodo = state.data;
console.log("====================================");

return {
data: [...tempTodo, { name: name, email: email, phone: phone }]
};
};

//这是商店用的

const mapDispatchToProps = dispatch => {
return bindActionCreators(
{
addToDos: (name, email, phone) =>
dispatch(toDoActions.addToDo(name, email, phone)),

toggleToDo: (index, name, email, phone) =>
dispatch(toDoActions.toggleToDo(index, name, email, phone))
},
dispatch
);
};

//这是按钮的点击功能

onButtonClick()
{
this.props.addToDos(name, email, phone);
}

// reducer 导出

export const reducer = createReducer(INITIAL_STATE, {
[Types.ADD_TO_DO]: addToDo,
});

最佳答案

试试这个:

export const addToDo = (state, action) => {
console.log(">>>>>>>>>>>>>>>>>>>>>>>>>>");
const { name, email, phone } = action;
const tempTodo = state.data || [];
console.log("====================================");
let newData = tempTodo;
const duplicate = tempTodo.find(todo => todo.name === name && todo.email === email && todo.phone ==== phone);
if(!duplicate) {
newData = [...tempTodo, { name: name, email: email, phone: phone }]
}
return {
data: newData
};
};

关于javascript - 数据重复并且输出显示两次。可能存在传播错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54104840/

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