There are currently countless numbers of state management libraries out there. This blog will show you how to use state management for React Native in Nx monorepo with TanStack Query (which happens to use Nx on their repo) and Redux.
This blog will show:
How to set up these libraries and their dev tools
How to build the sample page below in React Native / Expo with state management
How to do unit testing
It will call an API and show a cat fact on the page, allowing users to like or dislike the data.
Note: the React Query Devtools currently do not support react native, and it only works on the web, so there is a condition: { Platform.OS === ‘web’ && <ReactQueryDevtools />}.
For the react native apps, in order to use this tool, you need to use react-native-web to interpolate your native app to the web app first.
If you open my Expo app on the web by running nx start cats and choose the options Press w │ open web, you should be able to use the dev tools and see the state of my react queries:
Create a Query
What is a query?
“A query is a declarative dependency on an asynchronous source of data that is tied to a unique key. A query can be used with any Promise-based method (including GET and POST methods) to fetch data from a server.” (https://tanstack.com/query/v4/docs/react/guides/queries)
Now let’s add our first query. In this example, it will be added underlib/queries folder. To create a query to fetch a new fact about cats, run the command:
Now notice under libs folder, use-cat-fact folder got created under libs/queries:
If you use React Native CLI, just add a folder in your workspace root.
For this app, let’s use this API: https://catfact.ninja/. At libs/queries/use-cat-fact/src/lib/use-cat-fact.ts, add code to fetch the data from this API:
Essentially, you have created a custom hook that calls useQuery function from the TanStack Query library.
Unit Testing
If you render this hook directly and run the unit test with the command npx nx test queries-use-cat-fact, this error will show up in the console:
Invalid hook call. Hooks can only be called inside of the body of a function component. This could happen for one of the following reasons:
1. You might have mismatching versions of React and the renderer (such as React DOM)
2. You might be breaking the Rules of Hooks
3. You might have more than one copy of React in the same app
See https://reactjs.org/link/invalid-hook-call for tips about how to debug and fix this problem.
To solve this, you need to wrap your component inside the renderHook function from @testing-library/react-native library:
1. Install Library to Mock Fetch
Depending on which library you use to make HTTP requests. (e.g. fetch, axios), you need to install a library to mock the response.
If you use fetch to fetch data, you need to install jest-fetch-mock.
If you use axios to fetch data, you need to install
axios-mock-adapter.
For this example, since it uses fetch, you need to install jest-fetch-mock:
To test out useQuery hook, you need to wrap it inside a mock QueryClientProvider. Since this mock query provider is going to be used more than once, let’s create a library for this wrapper:
Then this is what the unit test for my query would look like:
import{TestWrapper}from'@nx-expo-monorepo/queries/test-wrapper';import{renderHook,waitFor}from'@testing-library/react-native';import{useCatFact}from'./use-cat-fact';importfetchMockfrom'jest-fetch-mock';describe('useCatFact',()=>{afterEach(()=>{jest.resetAllMocks();});it('status should be success',async ()=>{// simulating a server response fetchMock.mockResponseOnce(JSON.stringify({fact:'random cat fact',}));const{result}=renderHook(()=>useCatFact(),{wrapper:TestWrapper,});result.current.refetch();// refetching the query expect(result.current.isLoading).toBeTruthy();awaitwaitFor(()=>expect(result.current.isLoading).toBe(false));expect(result.current.isSuccess).toBe(true);expect(result.current.data).toEqual('random cat fact');});it('status should be error',async ()=>{fetchMock.mockRejectOnce();const{result}=renderHook(()=>useCatFact(),{wrapper:TestWrapper,});result.current.refetch();// refetching the query expect(result.current.isLoading).toBeTruthy();awaitwaitFor(()=>expect(result.current.isLoading).toBe(false));expect(result.current.isError).toBe(true);});});
If you use axios, your unit test would look like this:
import{TestWrapper}from'@nx-expo-monorepo/queries/test-wrapper';import{renderHook,waitFor}from'@testing-library/react-native';import{useCatFact}from'./use-cat-fact';importaxiosfrom'axios';importMockAdapterfrom'axios-mock-adapter';// This sets the mock adapter on the default instanceconstmockAxios=newMockAdapter(axios);describe('useCatFact',()=>{afterEach(()=>{mockAxios.reset();});it('status should be success',async ()=>{// simulating a server responsemockAxios.onGet().replyOnce(200,{fact:'random cat fact',});const{result}=renderHook(()=>useCatFact(),{wrapper:TestWrapper,});result.current.refetch();// refetching the queryexpect(result.current.isLoading).toBeTruthy();awaitwaitFor(()=>expect(result.current.isLoading).toBe(false));expect(result.current.isSuccess).toBe(true);expect(result.current.data).toEqual('random cat fact');});it('status should be error',async ()=>{mockAxios.onGet().replyOnce(500);const{result}=renderHook(()=>useCatFact(),{wrapper:TestWrapper,});result.current.refetch();// refetching the queryexpect(result.current.isLoading).toBeTruthy();awaitwaitFor(()=>expect(result.current.isLoading).toBe(false));expect(result.current.isError).toBe(true);});});
Notice that this file imports TestWrapper from @nx-expo-monorepo/queries/test-wrapper, and it is added to renderHook function with { wrapper: TestWrapper }.
Now you run the test command nx test queries-use-cat-fact, it should pass:
PASS queries-use-cat-fact libs/queries/use-cat-fact/src/lib/use-cat-fact.spec.ts (5.158 s)
useCatFact
✓ status should be success (44 ms)
✓ status should be error (96 ms)
Integrate with Component
Currently userQuery returns the following properties:
isLoading or status === 'loading' - The query has no data yet
isError or status === 'error' - The query encountered an error
isSuccess or status === 'success' - The query was successful and data is available
Now with components controlled by the server state, you can leverage the above properties and change your component to follow the below pattern:
Every time the “like” button gets clicked, you want to store the content of what users liked. So you need to create an entity to store this information.
Now you have passed the like function to the props of the facts component. Now inside the facts component, you can call the like function from props to dispatch the like action.
Debugging
To debug redux with Expo, I can simply open the Debugger Menu by entering “d” in the console or in the app, then choose the option “Open JS Debugger”.
Then you can view my redux logs in the JS Debugger console:
You can see the redux logs in console now:
Or you can run nx serve cats to launch the app in web view. Then you can use Redux Devtools and debug the native app like a web app:
Summary
Here is a simple app that uses TanStack Query and Redux for state management. These 2 tools are pretty powerful and they manage both server and client state for you, which is easy to scale, test, and debug.
Nx is a powerful monorepo tool. Together with Nx and these 2 state management tools, it will be very easy to scale up any app.