استفاده از قالب مخصوص Redux Toolkit جهت ایجاد پروژههای React/Redux
نویسنده: سیروان عفیفی
تاریخ: ۱۳۹۸/۱۲/۱۳ ۱۱:۵۵
آدرس: www.dntips.ir
| مطالب | ۳۶۹۴ |
| نویسندگان | ۲۷۶ |
| گروههای مطالب | ۱۰۲۴ |
| نقشههای راه | ۱۱۹ |
| دورهها | ۱۴ |
| اشتراکها | ۱۷۹۱۴ |
> npx create-react-app redux-template --template redux > cd redux-template > yarn start
import { configureStore } from '@reduxjs/toolkit';
import counterReducer from './features/counter/counterSlice';
export default configureStore({
reducer: {
counter: counterReducer,
},
}); Counter Counter.module.css counterSlice.js
import React, { useState } from 'react';
import { useSelector, useDispatch } from 'react-redux';
import {
decrement,
increment,
incrementByAmount,
selectCount,
} from './counterSlice';
import styles from './Counter.module.css';
export function Counter() {
const count = useSelector(selectCount);
const dispatch = useDispatch();
const [incrementAmount, setIncrementAmount] = useState(2);
return (
<div>
<div className={styles.row}>
<button
className={styles.button}
aria-label="Increment value"
onClick={() => dispatch(increment())}
>
+
</button>
<span className={styles.value}>{count}</span>
<button
className={styles.button}
aria-label="Decrement value"
onClick={() => dispatch(decrement())}
>
-
</button>
</div>
<div className={styles.row}>
<input
className={styles.textbox}
value={incrementAmount}
onChange={e => setIncrementAmount(e.target.value)}
/>
<button
className={styles.button}
onClick={() =>
dispatch(
incrementByAmount({ amount: Number(incrementAmount) || 0 })
)
}
>
Add Amount
</button>
</div>
</div>
);
} import { createSlice } from '@reduxjs/toolkit';
export const slice = createSlice({
name: 'counter',
initialState: {
value: 0,
},
reducers: {
increment: state => {
// Redux Toolkit allows us to 'mutate' the state. 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;
},
decrement: state => {
state.value -= 1;
},
incrementByAmount: (state, action) => {
state.value += action.payload.amount;
},
},
});
export const selectCount = state => state.counter.value;
export const { increment, decrement, incrementByAmount } = slice.actions;
export default slice.reducer; return {
...state,
models: state.models.map(c =>
c.model === action.payload.model
? {
...c,
on: action.payload.toggle
}
: c
)
}; state.models.forEach(item => {
if (item.model === action.payload.model) {
item.on = action.payload.toggle;
}
}); reducers: {
addTodo: {
reducer: (state, action: PayloadAction<Item>) => {
state.push(action.payload)
},
prepare: (text: string) => {
const id = nanoid()
return { payload: { id, text } }
},
}, reducers: {
todoAdded: (state: Todo[], action: Action) => {
state.push(action.payload);
},
} type Todo = {
id?: string;
name: string;
createdAt?: Date;
}; reducers: {
todoAdded: {
reducer: (state: Todo[], action: PayloadAction<Todo>) => {
state.push(action.payload);
},
prepare: (todo: Todo) => {
const id = nanoid();
return { payload: { id, name: todo.name, createdAt: new Date() } };
},
},
},