0

I have an application where I use a click counter.

const App = () => { const [nr, setNr] = useState(1); return ( <div> <p>{nr}</p> <Pagination defaultCurrent={nr} total={50} /> <button onClick={() => setNr(nr + 1)}>click</button> </div> ); }; 

Depending on the value the defaultCurrent should change in my Pagination component, but it is not working even the values are changing. Why the pages don't change when I click on the counter?

Demo: https://codesandbox.io/s/basic-ant-design-demo-ni423?file=/Test.js:183-386

3
  • The sandbox has an error. You're not rendering the button and p Commented Apr 17, 2020 at 8:15
  • 1
    <Pagination current={nr} total={50} /> Commented Apr 17, 2020 at 8:20
  • @palaѕн, but why after i click on the button click (for example till 3), and after that if i click on the button 1 , the active also still and 3? Commented Apr 17, 2020 at 8:50

2 Answers 2

1

defaultCurrent is only initialized on mount, therefore you should use the current property.

See example:

Edit Basic - Ant Design Demo

Sign up to request clarification or add additional context in comments.

3 Comments

but why after i click on the button click (for example till 3), and after that if i click on the button 1 , the active also still and 3?
I added a codesandbox to the answer, check it, you should use onChange for it to work
Ok, i will accept your ansewr, but could you take a look on my another question, please? stackoverflow.com/questions/61265277/solve-pagination-issues/…
1

You should use current prop:

const App = () => { const [nr, setNr] = useState(1); return ( <div> <p>{nr}</p> <Pagination current={nr} total={50} /> <button onClick={() => setNr(nr + 1)}>click</button> </div> ); }; 

1 Comment

but why after i click on the button click (for example till 3), and after that if i click on the button 1 , the active also still and 3?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.