Difference between revisions of "Redux + React with Redux Toolkit"

From Logic Wiki
Jump to: navigation, search
(Created page with "Category:Redux Category:React == Installing == bun add @reduxjs/toolkit == Creating Slices (/src/store/project.js) == <pre> import { createSlice } from '@reduxjs/to...")
 
 
Line 2: Line 2:
 
[[Category:React]]
 
[[Category:React]]
  
== Installing ==
+
== Example Project with Readme ==
bun add @reduxjs/toolkit
+
[https://github.com/Aliiybar/react-redux-example https://github.com/Aliiybar/react-redux-example]
 
+
== Creating Slices (/src/store/project.js) ==
+
<pre>
+
import { createSlice } from '@reduxjs/toolkit'
+
 
+
let lastId = 0;
+
 
+
const slice = createSlice ({
+
  name: 'projects'
+
  initialState: [],
+
  reducers: {
+
    projectAdded: (projects, action) => {
+
      projects.push({
+
        id: ++lastId,
+
        name: action.payload.name
+
      })
+
    }
+
  }
+
})
+
export { projectAdded } = slice.actions;
+
export default slice.reducer;
+
</pre>
+
 
+
== Configure Store (/src/store/configureStore) ==
+
<pre>
+
import { configureStore } from "@reduxjs/toolkit";
+
import reducer from "./projects"
+
 
+
export default function() {
+
  return configureStore({ reducer })
+
}
+
</pre>
+
 
+
== /src/index.js ==
+
<pre>
+
import configureStore from "./store/configureStore"
+
import * as actions from "./store/bugs"
+
import {projectAdded} from "./store/projects"
+
 
+
const store = configureStore()
+
 
+
store.subscribe(() => {
+
  console.log("Store changed ");
+
});
+
 
+
store.dispatch(actions.bugAdded({ description: "Bug 1"})
+
 
+
store.dispatch(projectAdded({ name: "Project 1"})
+
 
+
console.log(store.getState());
+
</pre>
+

Latest revision as of 15:40, 10 February 2026


Example Project with Readme

https://github.com/Aliiybar/react-redux-example