Redux is a predictable state container for JavaScript apps. It helps you write applications that behave consistently, run in different environments (client, server, and native), and are easy to test. On top of that, it provides a great developer experience, such as live code editing combined with a time traveling debugger. You can use Redux together with React, or with any other view library. It is tiny (2kB, including dependencies), but has a large ecosystem of addons available.
Features of React.js: There are unique features are available on React because that it is widely popular.
npm install @reduxjs/toolkit react-redux
mkdir store && cd store && type NUL > index.jsx && mkdir slices && cd slices && type NUL > Userslices.jsx
import {createSlice} from "@reduxjs/toolkit";
const userSlice = createSlice({
name: "user",
initialState: [],
reducers: {
addUser(state, action) {
state.push(action.payload)
},
removeUser(state, action) { },
deleteUsers(state, action) { },
}
});
export default userSlice.reducer;
export const {addUser,removeUser,deleteUsers}=userSlice.actions;
import { configureStore } from "@reduxjs/toolkit";
const store=configureStore({
reducer:{
user:userSlice,
},
});
export default store;
import {Provider} from 'react-redux'
import store from './store/index.jsx'
ReactDOM.createRoot(document.getElementById('root')).render(
<Provider store={store}>
<App />
</Provider>,
)
import {useDispatch} from 'react-redux'
const dispatch=useDispatch();
const addNewUser=(e)=>{
dispatch(addUser(e));
};
import { useSelector } from 'react-redux'
const data =useSelector((state)=>state.user)
extraReducers(builder){
builder.addCase(userSlice.actions.deleteUsers,()=>{
return [];
})
}
import { createAction } from "@reduxjs/toolkit";
export const clearAllUsers=createAction('clearAllUsers');
extraReducers(builder){
builder.addCase(clearAllUsers,()=>{
return [];
})
}