mirror of
https://gitlab.com/djdietrick/docs
synced 2026-05-03 00:20:54 -04:00
135 lines
3.3 KiB
Markdown
135 lines
3.3 KiB
Markdown
# Redux
|
|
|
|
Redux is a centralized state store that can be accessed from any component in your application. It acts as basically a centralized reducer, where it has a state and an dispatch method to update that state. Redux Toolkit is a library that simplifies creating actions to interact with the store, and is the recommended approach going forward.
|
|
|
|
```ts
|
|
// store/slices/song.ts
|
|
import { createSlice } from "@reduxjs/toolkit";
|
|
import { reset } from "../actions";
|
|
|
|
const songsSlice = createSlice({
|
|
name: "song",
|
|
initialState: [],
|
|
reducers: {
|
|
addSong(state, action) {
|
|
state.push(action.payload);
|
|
},
|
|
removeSong(state, action) {
|
|
// action.payload === string, the song we want to remove
|
|
const index = state.indexOf(action.payload);
|
|
state.splice(index, 1);
|
|
},
|
|
},
|
|
// listen to manual actions not created by this generator
|
|
extraReducers(builder) {
|
|
builder.addCase(reset, (state, action) => {
|
|
return [];
|
|
});
|
|
},
|
|
});
|
|
|
|
export const { addSong, removeSong } = songsSlice.actions;
|
|
// can also export this as the default
|
|
export const songsReducer = songsSlice.reducer;
|
|
```
|
|
|
|
```ts
|
|
// store/actions.ts
|
|
// Declare common actions shared by multiple reducers
|
|
import { createAction } from "@reduxjs/toolkit";
|
|
|
|
export const reset = createAction("app/reset");
|
|
```
|
|
|
|
```ts
|
|
// store/index.ts
|
|
import { configureStore } from "@reduxjs/toolkit";
|
|
import { songsReducer, addSong, removeSong } from "./slices/songsSlice";
|
|
import { reset } from "./actions";
|
|
|
|
const store = configureStore({
|
|
reducer: {
|
|
songs: songsReducer,
|
|
},
|
|
});
|
|
|
|
// Export store and slice actions
|
|
export { store, reset, addSong, removeSong };
|
|
```
|
|
|
|
```ts
|
|
// index.ts
|
|
import { createRoot } from "react-dom/client";
|
|
import { Provider } from "react-redux";
|
|
import App from "./App";
|
|
import { store } from "./store";
|
|
|
|
const rootElement = document.getElementById("root");
|
|
const root = createRoot(rootElement);
|
|
|
|
root.render(
|
|
<Provider store={store}>
|
|
<App />
|
|
</Provider>
|
|
);
|
|
```
|
|
|
|
```tsx
|
|
// components/songs.tsx
|
|
import { useDispatch, useSelector } from "react-redux";
|
|
import { createRandomSong } from "../data";
|
|
import { addSong, removeSong } from "../store";
|
|
|
|
function SongPlaylist() {
|
|
// object to call actions
|
|
const dispatch = useDispatch();
|
|
|
|
// fetch state
|
|
// can also filter/map state within here if you want
|
|
const songPlaylist = useSelector((state) => {
|
|
return state.songs;
|
|
});
|
|
|
|
// dispatch actions with some payload
|
|
const handleSongAdd = (song) => {
|
|
dispatch(addSong(song));
|
|
};
|
|
const handleSongRemove = (song) => {
|
|
dispatch(removeSong(song));
|
|
};
|
|
|
|
const renderedSongs = songPlaylist.map((song) => {
|
|
return (
|
|
<li key={song}>
|
|
{song}
|
|
<button
|
|
onClick={() => handleSongRemove(song)}
|
|
className="button is-danger"
|
|
>
|
|
X
|
|
</button>
|
|
</li>
|
|
);
|
|
});
|
|
|
|
return (
|
|
<div className="content">
|
|
<div className="table-header">
|
|
<h3 className="subtitle is-3">Song Playlist</h3>
|
|
<div className="buttons">
|
|
<button
|
|
onClick={() => handleSongAdd(createRandomSong())}
|
|
className="button is-link"
|
|
>
|
|
+ Add Song to Playlist
|
|
</button>
|
|
</div>
|
|
</div>
|
|
<ul>{renderedSongs}</ul>
|
|
</div>
|
|
);
|
|
}
|
|
|
|
export default SongPlaylist;
|
|
```
|