Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

223
Views
React redux - useSelect() and useDispatch() when mapping objects
//state.buttons is an array - it is used to map buttons to a menu

function NavMenu() {
  const buttons = useSelector((state) => state.buttons);
  const dispatch = useDispatch();

  const navButtons = buttons.map((button) => {
    const checkDirection = (button) => {
      if (button.pageNo >= button.prevPageNo) {
        dispatch(updateButtonsData({ newDirection: 1 }));
      } else {
        dispatch(updateButtonsData({ newDirection: -1 }));
      }
    };
    checkDirection(button);

    return (
      <li key={button.id} >
          <NavButton buttonData={button} />
      </li>
    );
  });

  return <ul>{navButtons}</ul>;
}

export default NavMenu;


//buttonSlice 

const initialState = [
  {
    id: 'homeId',
    prevPageNo: null,
    pageNo: 1,
    direction: 1,
    firstAnimFrame: 1,
    lastAnimFrame: 1,
  },
  {
    id: 'aboutId',
    prevPageNo: null,
    pageNo: 2,
    direction: 1,
    firstAnimFrame: 1,
    lastAnimFrame: 11,
  },{...}

const buttonsSlice = createSlice({
  name: 'ui',
  initialState,
  reducers: {
    updateButtonsData: {
      reducer(state, action) {
        state = action.payload;
      },
      prepare(
        id,
        newPrevPageNo,
        pageNo,
        newDirection,
        newFirstFrame,
        newLastFrame
      ) {
        return {
          payload: {
            id,
            prevPageNo: newPrevPageNo,
            pageNo,
            direction: newDirection,
            firstAnimFrame: newFirstFrame,
            lastAnimFrame: newLastFrame,
};},},},},});

Hello, I have a navigation menu where several parameters must be dynamically updated within the UI when a user presses a button. Each button will behave differently depending on the previous button as pressed (order of pressed buttons matters). My solution is to compare the initial state of the nav buttons array to each subsequent remapping. I thought it would be good to store the history of the previously pressed button as a value inside the nav objects array. Each time the navigation is refreshed, new parameters are added and accessible as props inside the mapped button and can then be accessed onClick.

Currently, the dispatch function does not work with my code, and the mapped buttons array remains unchanged.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

I assume that you are "triggering" the update data via checkDirection(button); and not onClick Event.

first this is a "no-go" code:

  const navButtons = buttons.map((button) => {
    const checkDirection = (button) => {
      if (button.pageNo >= button.prevPageNo) {
        dispatch(updateButtonsData({ newDirection: 1 }));
      } else {
        dispatch(updateButtonsData({ newDirection: -1 }));
      }
    };
    checkDirection(button);

    return (
      <li key={button.id} >
          <NavButton buttonData={button} />
      </li>
    );
  });

  return <ul>{navButtons}</ul>;
}

If you want to trigger a function while rendering an element, put it inside useEffect and not "outside".

So, transfer that code inside NavButton like:

    const checkDirection = useCallback(button) => {
      if (button.pageNo >= button.prevPageNo) {
        dispatch(updateButtonsData({ newDirection: 1 }));
      } else {
        dispatch(updateButtonsData({ newDirection: -1 }));
      }
    }, []);

   useEffect(() => checkDirection(button), [checkDirection, button]);
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!