Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set up React Query to simplify state management #5001

Merged
merged 15 commits into from
Aug 12, 2024
Merged

Conversation

p2edwards
Copy link
Contributor

@p2edwards p2edwards commented Jul 8, 2024

Details

This branch sets up React Query and associated tools.

  • ESLint: Add linters for React, React Hooks, and React Query
    • Install and configure React-related linters
    • Invoke the new React Renderer in code where it was missing
    • Note and demote some React-related errors to warnings so CI can pass today
    • Move formatting-related linter rules to a new section to make them easier to work with
  • React Query: Set up React Query
    • React query was installed proactively in #4992
    • Configure react query, adding a provider and queryClient
    • Install the react query devtools, for use in dev mode
  • Install related packages to make them available for upcoming PR branches

Builds upon this PR stack:

Related issues

Follow up to #4985
Follow up to #4992

Description

Set up React Query to simplify state management on new features. Install additional linter plugins and tools related to React and React Query.

Checklist

  1. If you've added code that should be tested, add tests
  2. If you've changed APIs, update (or create!) the documentation
  3. Ensure the tests pass
  4. Make sure that your code lints and that you've followed our coding style
  5. Write a title and, if necessary, a description of your work suitable for publishing in our release notes
  6. Mention any related issues in this repository (as #ISSUE) and in other repositories (as kobotoolbox/other#ISSUE)
  7. Open an issue in the docs if there are UI/UX changes

@p2edwards p2edwards changed the base branch from beta to eslint-type-duplicate-imports July 8, 2024 17:12
@p2edwards p2edwards added UI & UX User interface problems and improvements workflow Related to development process dependencies Pull requests that update a dependency file Front end labels Jul 8, 2024
p2edwards added 5 commits July 9, 2024 11:29
https://tanstack.com/query/latest/docs/framework/react/devtools

Click the toggle button (bottom right) to open the React Query pane.
By default, the button is perhaps overly eye-catching, so I faded it.
Proactively installing so it's available across collab workflow.

Wretch is a chainable, configurable `fetch` API wrapper.

- https://github.com/elbywan/wretch

Useful because:

- Throws errors by default, which is useful for React Query functions *
- Convenient chainable API - **succinct**
- Well-typed
- Small size
- Can cover our use cases in fetchWrapper and jQuery

*: https://tanstack.com/query/latest/docs/framework/react/guides/query-functions#usage-with-fetch-and-other-clients-that-do-not-throw-by-default
Immer makes immutable data structures easier to work with.

- https://immerjs.github.io/immer/

Useful because there are places in React and React Query where you need
to create a copy of an immutable object with some changes, and Immer
can be much easier to write and read than the equivalent spread syntax.

- updating state from useState, or using reducers (React)
  https://react.dev/learn/updating-objects-in-state#write-concise-update-logic-with-immer
  https://react.dev/learn/updating-arrays-in-state#write-concise-update-logic-with-immer
  https://react.dev/learn/extracting-state-logic-into-a-reducer#writing-concise-reducers-with-immer

- updating a state cache after mutation to avoid re-fetching, or to
  show optimistic updates (React Query)
  https://tanstack.com/query/latest/docs/framework/react/guides/updates-from-mutation-responses#immutability
  https://tanstack.com/query/latest/docs/framework/react/guides/optimistic-updates#via-the-cache
@p2edwards p2edwards changed the title WIP: Start using React Query to simplify state management Set up React Query to simplify state management Jul 10, 2024
@p2edwards p2edwards requested a review from magicznyleszek July 10, 2024 16:07
@p2edwards p2edwards self-assigned this Jul 10, 2024
@magicznyleszek magicznyleszek self-assigned this Jul 17, 2024
Base automatically changed from eslint-type-duplicate-imports to remove-context-types July 17, 2024 09:21
Base automatically changed from remove-context-types to beta July 18, 2024 21:07
jsapp/js/app.jsx Outdated
Lower button opacity to be less distracting
if it shows up in a screenshot. */}
<style>{'.tsqd-open-btn-container { opacity: 0.1 !important; };'}</style>
<ReactQueryDevtools />
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As per our previous discussion, I think we agreed to comment this out and then merge the PR :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@p2edwards p2edwards merged commit ee582a0 into beta Aug 12, 2024
4 checks passed
@p2edwards p2edwards deleted the react-query-start branch August 12, 2024 15:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
dependencies Pull requests that update a dependency file Front end UI & UX User interface problems and improvements workflow Related to development process
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants