Redux Logic
Redux Logic
Redux is a state management library for JavaScript apps, providing a centralized store to
manage the state of your application predictably and consistently.
Actions:
Actions are plain JavaScript objects that describe what happened in the application. They
usually have a type and payload.
Reducers:
Reducers are pure functions that take the current state and an action as arguments, and return
a new state. They specify how the application's state changes in response to actions.
Store:
The store is the object that brings actions and reducers together.
Types:
Types are constants that define the type of actions. They help avoid typos and make the code
more maintainable.
Action File(userActions.js) :
Reducer File(userReducer.js) :
const initialState = {
users: [],
};
Store.js:
import { createStore, combineReducers, applyMiddleware } from 'redux';
import thunk from 'redux-thunk';
import { composeWithDevTools } from 'redux-devtools-extension';
import userReducer from './reducers/userReducer';
Type File(type.js):
export const ADD_USER = 'ADD_USER';
Component File(helloComponent):
return (
<div>
<button onClick={handleAddUser}>Add User</button>
<ul>
{users.map((user, index) => (
<li key={index}>{user.name}</li>
))}
</ul>
</div>
);
};
try {
const response = await fetch('https://api.example.com/users');
const data = await response.json();