即使仅在一个函数中调度一次,Redux也会多次调度动作

时间:2020-05-03 14:00:49

标签: javascript reactjs redux reducers

因此,我想直接将最初为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}

优先级降低器有问题吗?

1 个答案:

答案 0 :(得分:1)

您正在调用 onClick处理程序中的changePriority函数。只需传递功能参考

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