Getting Started with Redux

Realted Doc

Official Documentation
https://redux.js.org/introduction/getting-started

Method 1. Basic Example

1.1 Create Project

1
pnpm create vite my-react-demo --template react 

1.2 Add Dependencies

1
pnpm install @reduxjs/toolkit react-redux

1.3 Create and use Redux Store

Create store Directory in src/ Directory.

Create index.js in store Directory.

1
2
3
4
5
import { configureStore } from '@reduxjs/toolkit'

export default configureStore({
reducer: {} //
})

Create counterSlice.js in store Directory.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
import { createSlice } from "@reduxjs/toolkit";

export const counterSlice = createSlice({
name: 'counter',
initialState: {
value: 0,
},
reducers: {
increment: (state) => {
state.value += 1
},
decrement: (state) => {
state.value -= 1
},
incrementByAmount: (state, action) => {
state.value += action.payload
}
}
})

export const { increment, decrement } = counterSlice.actions
export default counterSlice.reducer;

And then back to store/index.js. This file introduces the files that were just created.

1
2
3
4
5
6
7
8
import { configureStore } from "@reduxjs/toolkit";
import counterReducer from "./counterSlice";

export const store = configureStore({
reducer: {
counter: counterReducer
}
})

Next Step was to introduce the warehouse across the board.
Adjust src/main.jsx.

1
2
3
4
5
6
7
8
9
10
11
import { createRoot } from 'react-dom/client'
import App from './App.jsx'

import { Provider } from 'react-redux'
import { store } from './store'

createRoot(document.getElementById('root')).render(
<Provider store={store}>
<App />
</Provider>
)

Finally, it’s used in the component.

App.js

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
import './App.css'
import { useSelector } from 'react-redux'
import { useDispatch } from 'react-redux'
import { decrement, increment, incrementByAmount } from './store/counterSlice'



function App() {
const counter = useSelector((state) => state.counter.value)
const dispatch = useDispatch()

return (
<div>
<h1>Counter: {counter}</h1>
<button onClick={() => dispatch(increment())}>+1</button>
<button onClick={() => dispatch(decrement())}>-1</button>
<button onClick={() => dispatch(incrementByAmount(5))}>+5</button>
</div>
)
}

export default App

Method 2. Legacy Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import { createStore } from 'redux'

/**
* This is a reducer - a function that takes a current state value and an
* action object describing "what happened", and returns a new state value.
* A reducer's function signature is: (state, action) => newState
*
* The Redux state should contain only plain JS objects, arrays, and primitives.
* The root state value is usually an object. It's important that you should
* not mutate the state object, but return a new object if the state changes.
*
* You can use any conditional logic you want in a reducer. In this example,
* we use a switch statement, but it's not required.
*/
function counterReducer(state = { value: 0 }, action) {
switch (action.type) {
case 'counter/incremented':
return { value: state.value + 1 }
case 'counter/decremented':
return { value: state.value - 1 }
default:
return state
}
}

// Create a Redux store holding the state of your app.
// Its API is { subscribe, dispatch, getState }.
let store = createStore(counterReducer)

// You can use subscribe() to update the UI in response to state changes.
// Normally you'd use a view binding library (e.g. React Redux) rather than subscribe() directly.
// There may be additional use cases where it's helpful to subscribe as well.

store.subscribe(() => console.log(store.getState()))

// The only way to mutate the internal state is to dispatch an action.
// The actions can be serialized, logged or stored and later replayed.
store.dispatch({ type: 'counter/incremented' })
// {value: 1}
store.dispatch({ type: 'counter/incremented' })
// {value: 2}
store.dispatch({ type: 'counter/decremented' })
// {value: 1}

Method 3. Template Project

3.1 Create App

1
npx create-next-app --example with-redux my-app

After it,

1
pnpm install

3.2 Start Project

1
pnpm start

3.3 Basic Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import { createSlice, configureStore } from '@reduxjs/toolkit'

const counterSlice = createSlice({
name: 'counter',
initialState: {
value: 0
},
reducers: {
incremented: state => {
// Redux Toolkit allows us to write "mutating" logic in reducers. It
// doesn't actually mutate the state because it uses the Immer library,
// which detects changes to a "draft state" and produces a brand new
// immutable state based off those changes
state.value += 1
},
decremented: state => {
state.value -= 1
}
}
})

export const { incremented, decremented } = counterSlice.actions

const store = configureStore({
reducer: counterSlice.reducer
})

// Can still subscribe to the store
store.subscribe(() => console.log(store.getState()))

// Still pass action objects to `dispatch`, but they're created for us
store.dispatch(incremented())
// {value: 1}
store.dispatch(incremented())
// {value: 2}
store.dispatch(decremented())
// {value: 1}

Getting Started with Redux
https://www.hardyhu.cn/2024/12/24/Getting-Started-with-Redux/
Author
John Doe
Posted on
December 24, 2024
Licensed under