0

This doesn't work:

onClick={() => {
  header.column.getToggleSortingHandler();
}}

However if I invoke it like this then it works - why?

onClick={
  header.column.getToggleSortingHandler()
}

I was expecting the arrow function to give similar results.

0

1 Answer 1

1

getToggleSortingHandler returns a function that can be used to toggle this column's sorting state

In other words, getToggleSortingHandler is not the sort function, it creates sort function.

The first case doesn't work because you pass in a function that is not yet invoke the sort function

Working version:

onClick={() => {
  const sortFn = header.column.getToggleSortingHandler(); //only create sort function
  sortFn();//invoke the sorting
  //or header.column.getToggleSortingHandler()();
}}

The second case it works because when you invoke it once, the sort function is created and passed to onClick

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.