question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

setSort in listContext changes order if supplied with same SortPayload

See original GitHub issue

What you were expecting: When using the setSort from listContext it does not apply the correct values for repeated calls with the same object. I.e. calling setSort({ field: 'myField', order: 'DESC' }) twice will set order to ASC.

Steps to reproduce: I used the code of this page: https://marmelab.com/react-admin-demo/#/commands and added the setSort in the handleChange callback of TabbedDatagrid:

  const handleChange = useCallback(
    (_event: React.ChangeEvent<{}>, value: any) => {
      setFilters &&
        setFilters(
          { ...filterValues, value: value },
          displayedFilters,
          false // no debounce, we want the filter to fire immediately
        )
        setSort({ field: 'myField', order: 'DESC' })
    },
    [displayedFilters, filterValues, setFilters]
  )

Other information: Purpose: I want to adjust the sorting for each table when selected. Work-around was a check if the new sorting is different from the current one and only change if true.

      let newSort: SortPayload =
        value === myEnum.State1
          ? { field: 'myField', order: 'DESC' }
          : { field: 'myOtherField', order: 'DESC' }

      if (newSort.field != sort.field && newSort.order != sort.order) {
        setSort(newSort)
      }

Environment

  • React-admin version: 4.5.3
  • Last version that did not exhibit the issue (if applicable): n.a.
  • React version: 18
  • Browser: Chrome 107.0.5304.88

Issue Analytics

  • State:closed
  • Created 10 months ago
  • Comments:8 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
fzaninottocommented, Dec 2, 2022

Nice! Thanks for sharing your code.

0reactions
sebastianbuechlercommented, Dec 2, 2022

So I decided to implement the sophisticated approach outside of RA. Works like a charm and maybe it’s too much effort to integrate it into RA.

const TabbedDatagrid = () => {
  const listContext = useListContext()
  const { filterValues, setFilters, displayedFilters, setSort, sort } = listContext
  const totals = useGetTotals(filterValues) as any

  const [savedSorts, setSavedSorts] = React.useState<
    Partial<Record<MyEnum, SortPayload>>
  >({
    [MyEnum.ACTIVE]: { field: 'field1', order: 'ASC' },
    [MyEnum.CANCELLED]: {field: 'field1', order: 'DESC' },
    [MyEnum.ARCHIVED]: { field: 'field2', order: 'DESC' },
  })

  React.useEffect(() => {
    setSavedSorts({ ...savedSorts, [filterValues.value]: sort })
  }, [sort])

  const handleChange = useCallback(
    (_event: React.ChangeEvent<{}>, value: MyEnum) => {
      const savedSort = savedSorts[value]!

      const isNewList = value != filterValues.value
      const orderingWillBeDifferent = savedSort.field != sort.field || savedSort.order != sort.order
      if (isNewList && orderingWillBeDifferent) {
          setSort(savedSort)
      }

      setFilters &&
        setFilters(
          { ...filterValues, value: value },
          displayedFilters,
          false // no debounce, we want the filter to fire immediately
        )
    },
    [displayedFilters, filterValues, setFilters]
  )

  return ...
  }
Read more comments on GitHub >

github_iconTop Results From Across the Web

Sebastian Büchler sebastianbuechler - GitHub
Example of React Admin framework with JSON-Server. JavaScript ... setSort in listContext changes order if supplied with same SortPayload.
Read more >
Using filter in a custom list in React Admin - Stack Overflow
Handle filtering, sorting and pagination on local data. * * Returns the data and callbacks expected by <ListContext>.
Read more >
2D Sorting - Unity - Manual
Change the value of the Order in Layer to set the Renderer's priority among other Renderers within the same Sorting Layer.
Read more >
Issues · marmelab/react-admin · GitHub
Reexport context related apis from react-admin isntead of relying on 3rd parties ... setSort in listContext changes order if supplied with same SortPayload...
Read more >
Marmelab React-Admin Statistics & Issues - Codesti
setSort in listContext changes order if supplied with same SortPayload, closed, 8 ; NumberInput misinterprets zero-values (0 or 0.0) as type of string...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found