Gathering detailed insights and metrics for signal-react
Gathering detailed insights and metrics for signal-react
Gathering detailed insights and metrics for signal-react
Gathering detailed insights and metrics for signal-react
npm install signal-react
Typescript
Module System
Node Version
NPM Version
JavaScript (100%)
Total Downloads
0
Last Day
0
Last Week
0
Last Month
0
Last Year
0
2 Stars
14 Commits
1 Watchers
1 Branches
1 Contributors
Updated on Oct 13, 2023
Latest Version
1.0.3
Package Id
signal-react@1.0.3
Unpacked Size
11.33 kB
Size
3.55 kB
File Count
5
NPM Version
9.5.1
Node Version
18.16.0
Published on
Apr 18, 2023
Cumulative downloads
Total Downloads
Last Day
0%
NaN
Compared to previous day
Last Week
0%
NaN
Compared to previous week
Last Month
0%
NaN
Compared to previous month
Last Year
0%
NaN
Compared to previous year
1
This is a library for sending "signals" (state changes) between components, regardless of their hierarchical position.
1npm i signal-react
In each React component, you can use a hook called useSignal, to which you can pass the initial state value as a parameter, just like useState(). The difference with the latter is that useSignal should NOT be destructured in an array.
To use the useSignal hook, you can use it in a basic way by passing a value or a configuration object.
1const signal = useSignal(0); 2const signal = useSignal({});
1const config = { 2 initState: {/* initial state */}, 3 opts: { /* opts is optional */ 4 selectors: [ 5 { name: 'example1', path: 'example1.obj.obj.value' }, 6 { name: 'example2', path: 'example2' }, 7 { name: 'example3', path: 'example3' }, 8 ], 9 equalityFn: (a,b) => a===b 10 }, 11};
Each selector has a name that will be the identifier of the setter and getter (signal.name). The path allows you to point directly to a value within the state.
Each selector can be added an equalityFn to modify the default comparison function for each selector, which is a===b.
1 { name: 'example3', path: 'example3', equalityFn: (a,b)=> a.value === b.value },
"Importance of the comparison method 'equalityFn', this method is responsible for identifying whether there was a state change or not, if there was, a re-render will be triggered."
You can use the following methods for each created signal.
1const signal = useSignal({});
Methods for setting selectors.
signal.getSelectors(): returns an array with the names of all created selectors.
signal.addSelectors([{ name: 'example2', path: 'example2' }]): adds selectors to the signal.
signal.deleteSelector("name"): removes a selector.
These are the methods for getting values from the state.
*Optionally as a second parameter, we can send a function equalityFn to modify the comparison between state changes. getValue((state)=> state.obj.data, (a,b) => a===b).
*Optionally as a second parameter, we can send a function equalityFn to modify the comparison between state changes. getValue("obj.data", (a,b) => a===b).
Basic usage in a React component, in this example, the parent component will send a signal to the child and only the child will re-render.
1import { useSignal } from 'signal-react'; 2 3const Child = memo(({ signal }) => { 4 return <>{signal.value}</>; 5}); 6 7const Component = memo(() => { 8 const signal = useSignal(0); 9 10 const onClick = () => { 11 signal.value = (value) => value + 1; 12 }; 13 14 return ( 15 <> 16 <button onClick={onClick}>Summarize</button> 17 <Child signal={signal} /> 18 </> 19 ); 20});
In this case, the child component will send a signal to the parent, and only the parent will re-render. We must use memo to avoid propagating the render to the child.
1const Child = memo(({ signal }) => { 2 3 const onClick = () => { 4 signal.value = (value) => value + 1; 5 }; 6 7 return ( 8 <> 9 <button onClick={onClick}>Summarize</button> 10 </> 11 ); 12}); 13 14const Component = memo(() => { 15 const signal = useSignal(0); 16 17 return ( 18 <> 19 {signal.value} 20 <Child signal={signal} /> 21 </> 22 ); 23});
We can use the Signal class directly to handle state, this way we could avoid using useContext to have the same state and methods in different child components of the context.
1const signalInstance = new Signal(config.initState, config.opts); 2 3const Component = memo(() =>{ 4 return <>{signalInstance.value}<> 5});
We can also use the same instance to initialize useSignal
1const signalInstance = new Signal(config.initState, config.opts); 2 3const Component = memo(() =>{ 4 const signal = useSignal(signalInstance); 5 6 return <>{signal.value}<> 7});
Or we can pass the instance of a signal to other hooks to handle the same state.
1const signalInstance = new Signal(config.initState, config.opts); 2 3const Component = memo(() =>{ 4 const signal = useSignal(signalInstance); 5 6 return ( 7 <> 8 {signal.value} 9 <Child1/> 10 <Child2/> 11 </> 12 ); 13}); 14 15const Child1 = memo(() =>{ 16 const signal = useSignal(signalInstance); 17 18 return <>{signal.value}<> 19}); 20 21const Child2 = memo(() =>{ 22 const signal = useSignal(signalInstance); 23 24 return <>{signal.value}<> 25});
This way all the useSignal will handle the same state, and all of them will be "connected" to each other.
If we don't want to use the Signal class, we can connect different hooks in this way.
1const Component = memo(() =>{ 2 const signal1 = useSignal(config1); 3 const signal2 = useSignal(config2); 4 5 return ( 6 <> 7 {signal1.value} {/*Hook 1*/} 8 <Child1 signal={signal1}/> 9 <Child2 signal={signal2}/> 10 </> 11 ); 12}); 13 14const Child1 = memo(({signal}) =>{ 15 const signalChild = useSignal(signal); /*Hook 1*/ 16 return <>{signalChild.value}<> 17}); 18 19const Child2 = memo(({signal}) =>{ 20 const signalChild = useSignal(signal); /*Hook 2*/ 21 return <>{signalChild.value}<> 22});
No vulnerabilities found.
No security vulnerabilities found.