Gathering detailed insights and metrics for @rtbjs/use-state
Gathering detailed insights and metrics for @rtbjs/use-state
Gathering detailed insights and metrics for @rtbjs/use-state
Gathering detailed insights and metrics for @rtbjs/use-state
npm install @rtbjs/use-state
Typescript
Module System
Node Version
NPM Version
Total Downloads
0
Last Day
0
Last Week
0
Last Month
0
Last Year
0
3 Commits
1 Watchers
1 Branches
1 Contributors
Updated on Sep 29, 2023
Latest Version
1.0.7
Package Id
@rtbjs/use-state@1.0.7
Unpacked Size
679.36 kB
Size
83.59 kB
File Count
458
NPM Version
10.1.0
Node Version
20.9.0
Published on
Jun 27, 2024
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
2
20
@rtbjs/use-state
is a state management tool that can act as a local state and be easily turned into a global redux state. It is an innovative approach to state management that combines the advantages of both React's useState and Redux's state management.
To install @rtbjs/use-state
, simply use npm or yarn:
1npm install @rtbjs/use-state 2# or 3yarn add @rtbjs/use-state
When developing features in a React application, it's common to start with local state (using useState) and avoid incorporating Redux until later stages. However, this can lead to suboptimal state management as the application grows. To share state between components, developers may pass it as props and move state initialization to higher-level components. Redux useState simplifies the transition to global state and ensures efficient state management.
Redux useState resolves the issue mentioned above by facilitating the shift from local state to global state. Initially, you don't need to specify a name for your state; it behaves like React's useState. When the need arises to access the state elsewhere, you can assign it a name, making it a global state that uses Redux store. This allows the state to be accessed and modified in various components, with changes tracked by Redux DevTools.
To use Redux useState, wrap your application with the ToolBoxProvider provider, similar to how you wrap it with the Redux Provider.
1import { Provider } from 'react-redux'; 2import { store } from './store'; 3import { TestProject } from './test-project'; 4import { ToolBoxProvider } from '@rtbjs/use-state'; 5 6const TestProjectHOC = () => { 7 return ( 8 <Provider store={store}> 9 <ToolBoxProvider store={store}> 10 <TestProject /> 11 </ToolBoxProvider> 12 </Provider> 13 ); 14}; 15 16export default TestProjectHOC;
Add toolBoxEnhancer:
1import { configureStore } from '@reduxjs/toolkit'; 2import counterReducer from './features/counter/counter-slice'; 3import { withUseState } from '@rtbjs/use-state'; 4 5export const store = configureStore({ 6 reducer: withUseState({ 7 counter: counterReducer, 8 }), 9}); 10 11export type RootState = ReturnType<typeof store.getState>; 12export type AppDispatch = typeof store.dispatch;
Use to save state in your components:
1import { useState } from '@rtbjs/use-state'; 2import { TestProject2 } from './test-project2'; 3 4const TestProject = () => { 5 const [value, setValue] = useState({ 6 initialValue: 10, 7 }); 8 9 return ( 10 <div> 11 <button onClick={() => setValue((value || 0) - 1)}>Decrement</button> 12 <div style={{ fontSize: '30px' }}>{value}</div> 13 <button onClick={() => setValue((value || 0) + 1)}>Increment</button> 14 <TestProject2 /> 15 </div> 16 ); 17}; 18 19export { TestProject };
In the case above, the name is not set and the state is local. It cannot be accessed in other components. To make it global simply add a name:
1const [value, setValue] = useState({ 2 initialValue: 10, 3 name: 'myState', 4});
Now in all other components where myState needs to be used, you can simply add the same code and access and edit the shared state.
useState accepts an options parameter:
initialValue
(any, optional): The initial value of the state. The first mounted component sets it and it is not reset by other components unless forceInitialValue
is set to true
.name
(string, optional): Name of the state. If it is not provided, the state is local. If it is provided, the state is global and Redux is used.forceInitialValue
(boolean, optional): Will set or overwrite the initial value even if it has been set before the component is mounted.logs
(boolean, optional): Adds Redux DevTools logs for local state. In this case the state is given a random name. Logs are always on for global states.Please report any issues and feature requests to: issues tracker
No vulnerabilities found.
No security vulnerabilities found.