
Security News
Meet Socket at Black Hat and DEF CON 2025 in Las Vegas
Meet Socket at Black Hat & DEF CON 2025 for 1:1s, insider security talks at Allegiant Stadium, and a private dinner with top minds in software supply chain security.
react-reducer-ssr
Advanced tools
React Reducer SSR is a library that serves as an alternative to Redux for managing state in React applications. It leverages built-in React hooks and the context API, offering support for Server-Side Rendering (SSR), selectors, and async actions.
You can easily install react-reducer-ssr
using either Yarn or npm:
# Using Yarn
yarn add react-reducer-ssr
# Using npm
npm install react-reducer-ssr --save
To get started with react-reducer-ssr
, you need to import and configure it in your application. Here's a basic setup example:
import React from 'react';
import { ReducerProvider } from 'react-reducer-ssr';
import { reducers } from "./context";
function App() {
return (
<ReducerProvider reducer={reducers} initialState={{/* Your initial state */}}>
{/* Your application components */}
</ReducerProvider>
);
}
export default App;
react-reducer-ssr
allows you to create reducers to manage your application's state. Here's an example of creating a reducer:
import { TypedUseSelectorHook, useStateSelectorT, combineReducers } from 'react-reducer-ssr'
import { preferencesReducer } from './preferences.reducer'
import { usersReducer } from './users.reducer'
export const reducers = combineReducers({
preferences: preferencesReducer,
users: usersReducer
})
export type RootState = ReturnType<typeof reducers>
export const useStateSelector: TypedUseSelectorHook<RootState> = useStateSelectorT
import type { AnyAction } from "react-reducer-ssr";
export interface IUsersState {
userList?: string[]
}
export function usersReducer(draft: IUsersState, action: AnyAction): IUsersState {
switch (action.type) {
case 'GET_ALL_USERS': {
draft.userList = action.userList;
} break;
}
return draft
}
react-reducer-ssr
uses 'immer' internally, thus state mutation is allowed. No need to recreate a new object each time state changes.
Selectors help you access specific parts of your state. Here's how you can use selectors:
import { useStateSelector } from "./context";
const MyComponent = () => {
const users = useStateSelector(root => root.users);
return (
<div>
{/* Render your selected data */}
</div>
);
};
You can perform asynchronous actions with react-reducer-ssr
as well. Here's an example:
import { useDispatch } from 'react-reducer-ssr';
const MyComponent = () => {
const dispatch = useDispatch();
const fetchData = async () => {
await dispatch(usersActions.getUsers());
};
return (
<div>
<button onClick={fetchData}>Fetch Data</button>
</div>
);
};
export const usersActions = {
getUsers
}
async function getUsers(companyCode: string) {
try {
const response = await fetch("some_url");
const userList = await userList.json();
return { type: 'GET_ALL_USERS', userList }
}
catch(err) {
return { type: 'GET_ALL_USERS_FAILED', err }
}
}
react-reducer-ssr
offers support for Server-Side Rendering.
import { reducers } from "./context";
import { createServerStore, DispatchFunction } from 'react-reducer-ssr'
const store = createServerStore(reducers, {/*initial state here*/} as any);
...
await loadData(store.dispatch);
...
async function loadData(dispatch: DispatchFunction, cookies: any) {
await dispatch(userActions.getUsers());
}
For the complete example see react-ssr-ts-scss-rollup starter template.
We welcome contributions from the community! If you'd like to contribute to react-reducer-ssr
, please follow our contribution guidelines.
This project is licensed under the MIT License. For more information, see the LICENSE file.
For support and bug reports, please open an issue on GitHub.
FAQs
React reducer with server side rendering
The npm package react-reducer-ssr receives a total of 0 weekly downloads. As such, react-reducer-ssr popularity was classified as not popular.
We found that react-reducer-ssr demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers 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
Meet Socket at Black Hat & DEF CON 2025 for 1:1s, insider security talks at Allegiant Stadium, and a private dinner with top minds in software supply chain security.
Security News
CAI is a new open source AI framework that automates penetration testing tasks like scanning and exploitation up to 3,600× faster than humans.
Security News
Deno 2.4 brings back bundling, improves dependency updates and telemetry, and makes the runtime more practical for real-world JavaScript projects.