When defining a controlled sort state via the sorted prop, React table appears to be updating it's view based on it's internal state.
See https://codepen.io/anon/pen/VbNPda?editors=0010
In the above code snippet, rendering occurs on a fixed loop and the application state never changes.
If you click on the headers, you'll notice the sorting behaves erratically as it renders it's own internal state immediately followed by the controlled sort state.
One thought that occured to me: Is the sorted prop really a defaultSorted prop? Ie more like the behaviour of defaultValue and value on React's Input component.
A few things first:
sorted is a controlled prop, which overrides the internal state.onSortedChange is a callback that fires when the user changes the desired sorting model.So whats happening here is the following:
onSortedChange, and not immediately placing it in the sorted prop, the table does not detect a change on the sorted prop.sorted prop has not changed at all, and because of this, the table will default to using the sorting that did change from the internal state.To fix this problem, you would simply need to capture the sorting change from the callback, and replace the current sorted prop with the new value.
I understand that this may feel strange at first, and maybe it is. I won't technically consider this a bug, but it is an edge case that we could handle better. I'll keep this in consideration for the future, but I've provided a codepen that shows how to fix the issue: https://codepen.io/tannerlinsley/pen/zwVLKp?editors=0010
Most helpful comment
A few things first:
sortedis a controlled prop, which overrides the internal state.onSortedChangeis a callback that fires when the user changes the desired sorting model.So whats happening here is the following:
onSortedChange, and not immediately placing it in thesortedprop, the table does not detect a change on thesortedprop.sortedprop has not changed at all, and because of this, the table will default to using the sorting that did change from the internal state.To fix this problem, you would simply need to capture the sorting change from the callback, and replace the current
sortedprop with the new value.I understand that this may feel strange at first, and maybe it is. I won't technically consider this a bug, but it is an edge case that we could handle better. I'll keep this in consideration for the future, but I've provided a codepen that shows how to fix the issue: https://codepen.io/tannerlinsley/pen/zwVLKp?editors=0010