createWithEqualityFn test case in Zustand explained.
In this article, we will understand the test case written to validate the createWithEqualityFn that prevents re-render based on a condition and an equality function that you can pass.
// Prevent re-render if new value === 1. const item = useBoundStore( (s) => s.item, (_, newItem) => newItem.value === 1, )
useBoundStore accepts the selector and the equality function that is used to prevent re-render based on the matching value.
The above example in the basic.test is used to prevent re-render when the value is 1.
await findByText('renderCount: 1, value: 0')// This will not cause a re-render.act(() => setState({ item: { value: 1 } }))await findByText('renderCount: 1, value: 0')// This will cause a re-render.act(() => setState({ item: { value: 2 } }))await findByText('renderCount: 2, value: 2')
These assertions validate that state update does not cause any re-render.
Hey, my name is Ramu Narasinga. I study large open-source projects and create content about their codebase architecture and best practices, sharing it through articles, videos.