所以,我想直接将初始为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}
是不是优先级还原器有问题?
你是 呼叫 的 changePriority
函数的onClick处理程序中。只要传递函数引用
<button onClick={() => changePriority(1)}>1</button>
<button onClick={() => changePriority(2)}>0</button>