gpt4 book ai didi

javascript - Redux 会多次调度 action,即使它只在一个函数中调度一次

转载 作者:行者123 更新时间:2023-12-02 20:52:26 25 4
gpt4 key购买 nike

所以,我想直接将优先级的值(最初为1)替换为不同的值。我为它创建了一个 redux 函数,但它多次发送该函数。

行动


export const editPriority = (id, listID, newPriority) => {
return {
type: TRELLO_CONSTANTS.PRIORITY,
payload: { id, listID, newPriority },
};
};

case TRELLO_CONSTANTS.PRIORITY: {
const { id, newPriority } = action.payload;
const card = state[id];
card.priority = newPriority;
return { ...state, [`card-${id}`]: card };
}

export const TRELLO_CONSTANTS = {
PRIORITY: 'PRIORITY',
};

这是我的函数 -

import {editPriority} from './actionTypes.js';

const card=({dispatch})=> {
const [newPriority, priority] = useState();
}

const changePriority = (e) => {
// newPriority(e);
priority(e);
dispatch(editPriority(id, listID, priority));
console.log(priority);
};

// and the main function


<button onClick={changePriority(1)}>1</button>
<button onClick={changePriority(2)}>0</button>

{priority === 0 ? <p>0</p> : null}
{priority === 1 ? <p>1</p> : null}

优先级 reducer 有问题吗?

最佳答案

您正在 onClick 处理程序中调用changePriority 函数。只需传递函数引用即可

<button onClick={() => changePriority(1)}>1</button>
<button onClick={() => changePriority(2)}>0</button>

关于javascript - Redux 会多次调度 action,即使它只在一个函数中调度一次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61575679/

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