react-shared-state-maker
react hook for share state between the components without use context api.
Attention
The stable version is 1.3.x, earlier versions may not work properly.
Example: make shared state between the components
Make shared state hook in useSharedState.ts
/ useSharedState.js
:
; const useSharedState = ; ;
Put in ComponentA.tsx
/ ComponentA.js
:
import React from 'react';import useSharedState from './useSharedState'; const ComponentA = const value setValue = ; return <input = = /> ;; ;
And put in ComponentB.tsx
/ ComponentB.js
:
import React from 'react';import useSharedState from './useSharedState'; const ComponentB = const value setValue = ; return <input = = /> ;; ;
Now, put ComponentA and ComponentB in App.tsx
/ App.js
, let's all!
import React from 'react';import ComponentA from './ComponentA';import ComponentB from './ComponentB'; const App = return <> <ComponentA /> <ComponentB /> </> ;; ;
Example: set shared state out of component
Refer to previous section, ComponentA, ComponentB and useSharedState are prepared.
import React from 'react';import ComponentA from './ComponentA';import ComponentB from './ComponentB';import useSharedState from './useSharedState'; const setValue = useSharedStatecurrent;const clearContent = ; // will re-render only components of shared state const App = return <> <ComponentA /> <ComponentB /> <input ="button" ="clear" => </> ;; ;