Skip to content

Commit

Permalink
redux saga part
Browse files Browse the repository at this point in the history
  • Loading branch information
kuroidoruido committed May 27, 2024
1 parent e8d4b1d commit 9d4b36f
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 3 deletions.
Binary file added docs/assets/images/redux-saga.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
76 changes: 76 additions & 0 deletions docs/markdown/08-states/32-redux-saga.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,79 @@
##==##

# Redux Saga

![center](./assets/images/redux-saga.png)

##==##

<!-- .slide: class="with-code" -->

# Setup Saga

<!-- prettier-ignore -->
```typescript [1,5|3|8-9|12]
import createSagaMiddleware from 'redux-saga';

import appSaga from './sagas';

const sagaMiddleware = createSagaMiddleware()
const store = configureStore({
reducer,
middleware: (getDefaultMiddleware) =>
getDefaultMiddleware().concat([sagaMiddleware]),
});

sagaMiddleware.run(appSaga);
```

<!-- .element: class="big-code" -->

##==##

<!-- .slide: class="with-code" -->

# Dispatch some actions

<!-- prettier-ignore -->
```typescript []
function TodoList() {
const dispatch = useDispatch();
useEffect(() => dispatch(navigateTo('/')), []);
return <>
<Todo onRemoveClick={(id) => dispatch(todosSlice.remove(id))}>
<button onClick={() => dispatch(todosSlice.add())}>Add</button>
</>
}

```

<!-- .element: class="big-code" -->

##==##

<!-- .slide: class="with-code" -->

# Create a saga

<!-- prettier-ignore -->
```typescript [13,17|14-16|14|3-11|6|5,7,9]
import { call, put, takeEvery } from 'redux-saga/effects'

function* fetchTodos(action) {
try {
yield put({ type: 'loadTodos/fetching' });
const todos = yield call(Api.fetchTodos);
yield put({ type: 'loadTodos/successful', todos });
} catch (e) {
yield put({type: "loadTodos/failed", message: e.message});
}
}

function* mySaga() {
yield takeEvery(navigateTo.type, fetchTodos);
yield takeEvery(todosSlice.add.type, /* ... */);
yield takeEvery(todosSlice.remove.type, /* ... */);
}
```

<!-- .element: class="big-code" -->
9 changes: 6 additions & 3 deletions steps/api/db.json
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,10 @@
}
],
"messages": [
{ "id": "59b53c9015763cd4d9d2a4f2", "type": "info", "text": "New mission for Leanne Woodard at Planet Express" }
],
"config": { "apiBaseUrl": "/api", "theme": "pink" }
{
"id": "59b53c9015763cd4d9d2a4f2",
"type": "info",
"text": "New mission for Leanne Woodard at Planet Express"
}
]
}

0 comments on commit 9d4b36f

Please sign in to comment.