Security News
Node.js EOL Versions CVE Dubbed the "Worst CVE of the Year" by Security Experts
Critics call the Node.js EOL CVE a misuse of the system, sparking debate over CVE standards and the growing noise in vulnerability databases.
@rootstrap/redux-tools
Advanced tools
This package has some basic functionality common to both react bases. It includes a status reducer that lets you track the status of your async actions and a thunks-like middleware that will automatically dispatch success and failure actions.
This package provides an action creator utility, that together with the provided middleware will make it very easy to create side effects for your actions. This setup will automatically execute your side effect thunk and dispatch success or error actions when the thunk succeeds or fails, respectively.
createThunk
receives the action names prefix as the first argument and the async thunk as the second one.
Example:
// src/actions/userActions.js
import { createThunk } from '@rootstrap/redux-tools'
export const getProfile = createThunk(
'GET_PROFILE',
userService.getProfile,
);
You can then dispatch this getProfile
action, and the middleware will automatically dispatch actions with types GET_PROFILE_SUCCESS
or GET_PROFILE_ERROR
for you.
The returned object, (getProfile
in the example above) has 4 properties you can use in order to handle the different dispatched actions in your reducer:
Following the previous example:
// src/reducers/userReducer.js
import { getProfile } from 'src/actions/userActions';
const actionHandlers = {
[getProfile.success]: (state, { payload }) => {
state.user = payload;
},
};
If you need to access the store, or dispatch extra actions from your thunk, you can use dispatch
and getState
as the last two parameters.
Example:
Dispatching some custom analytics event that requires store data:
// src/actions/userActions.js
import { createThunk } from '@rootstrap/redux-tools'
export const getProfile = createThunk(
'GET_PROFILE',
async (userId, dispatch, getState) => {
const { analytics: { analyticsToken } } = getState();
const profile = await userService.getProfile(profileId);
dispatch(analytics.logProfile(analyticsToken, profile));
return profile;
},
);
To access status information on a component the useStatus
hook is provided.
The following status constants are exported:
Here is a simple example:
import { useStatus, useDispatch } from 'hooks';
import { getProfile } from 'src/actions/userActions';
import { SUCCESS, LOADING, ERROR } from '@rootstrap/redux-tools'
const MyComponent = () => {
const getProfileRequest = useDispatch(getProfile);
const { status, error } = useStatus(getProfile);
return <>
<button onClick={getProfileRequest}>Show profile!</button>
{(status === LOADING) && <Loading />}
{(status === SUCCESS) && <ProfileComponent />}
{(status === ERROR) && <ErrorComponent />}
</>
}
A useLoading
hook is also available if you only care about loading status. It returns a boolean indicating whether the action is still loading or not.
To reset the status of an action you can dispatch the reset
action returned by createThunk
.
npm i @rootstrap/redux-tools
or
yarn add @rootstrap/redux-tools
// src/reducers/index.js
import { combineReducers } from 'redux'
import { statusReducer } from '@rootstrap/redux-tools'
const rootReducer = combineReducers({
// ...your other reducers here
// you have to pass statusReducer under 'statusReducer' key,
statusReducer
})
import { createStore, applyMiddleware } from 'redux'
import { thunkMiddleware } from '@rootstrap/redux-tools'
import rootReducer from 'src/reducers/index'
const store = createStore(rootReducer, applyMiddleware(thunkMiddleware))
@rootstrap/redux-tools is available under the MIT license. See the LICENSE file for more info.
@rootstrap/redux-tools is maintained by Rootstrap with the help of our contributors.
FAQs
Redux tools we use in both react bases
The npm package @rootstrap/redux-tools receives a total of 16 weekly downloads. As such, @rootstrap/redux-tools popularity was classified as not popular.
We found that @rootstrap/redux-tools demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Critics call the Node.js EOL CVE a misuse of the system, sparking debate over CVE standards and the growing noise in vulnerability databases.
Security News
cURL and Go security teams are publicly rejecting CVSS as flawed for assessing vulnerabilities and are calling for more accurate, context-aware approaches.
Security News
Bun 1.2 enhances its JavaScript runtime with 90% Node.js compatibility, built-in S3 and Postgres support, HTML Imports, and faster, cloud-first performance.