因此,我想直接将最初为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}
优先级降低器有问题吗?
答案 0 :(得分:1)
您正在调用 onClick处理程序中的changePriority
函数。只需传递功能参考
<button onClick={() => changePriority(1)}>1</button>
<button onClick={() => changePriority(2)}>0</button>