0
//PaginationContainer.js
function PaginationContainer(props) {
  const { data = {} } = props;
  const {
    endPage = 5,
    currentPage = 1,
    nextPage = 6,
    prevPage = null,
    pagePerCount = 10,
    pageBlock = 5,
    totalData = 0,
  } = data;

  const PageState = {
    currentPage: currentPage || 1,
    pageNumbers: [],
    totalPage: 0,
  };
  const [values, setValues] = useImmer(PageState);
  console.log("state", PageState);<-every time change!
  console.log("values", values);<- previous value....:(

  ....
}
console.log 
state
    {currentPage: '1', pageNumbers: Array(0), totalPage: 0}

console.log 
values {currentPage: '8', pageNumbers: Array(10), totalPage: 16}

Hello? I always put new values ​​as props in the container to the pagination controller. However, the initial value of useInmmer does not change. Does anyone know why this is the case?

Evaldas Buinauskas
  • 13,739
  • 11
  • 55
  • 107
yooo
  • 63
  • 5
  • I don't fully understand your question or what you're expecting to happen. The whole purpose of Immer is to allow state mutative syntax while not actually mutating state, so I would never expect the initial state to be updated from a state change. – Brian Thompson Nov 12 '21 at 14:19

1 Answers1

0

you can use usEffect hook, to ensure values updated with latest changes, then setValues inside it

  useEffect(()=>{
    setValues(PageState);
  },[PageState, PageState.currentPage])
Ashish Kamble
  • 2,555
  • 3
  • 21
  • 29