-
Notifications
You must be signed in to change notification settings - Fork 6.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix #1518: Move root route configuration to routes.js, add documentation for creating a global saga #1545
Closed
Closed
Fix #1518: Move root route configuration to routes.js, add documentation for creating a global saga #1545
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
d800e79
Move root route configuration to routes.js, add documentation for cre…
d099381
ESLint spacing cleanup
97857ba
Merge branch 'dev' into dev
bb69de2
Update global saga documentation
ec72339
Simplified routes.js config, and updated global saga instructions.
2e57793
Merge branch 'dev' into dev
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
# `global saga` | ||
|
||
A global saga is an app level saga. The built-in example of sagas in this app are all contained within child containers that get put inside the `App` container. The app container by itself is pretty basic, allowing for all the action to happen within it's child containers; however, sometimes that's just not enough. Imagine the following scenario if you will. | ||
|
||
> You have an app that upon loading needs to grab data from the server, regardless of which route the user entered. You could put a function inside the `onComponentMount` of every single container, but that's not very DRY. What you end up with is a lot of duplicate constants, actions, and reducers. | ||
> | ||
> Enter the global saga. A way to load up a `sagas.js` file for the `App` container. | ||
|
||
## Usage | ||
|
||
We'll be doing the following: | ||
|
||
1. Adding `sagas.js` to `app/containers/App` directory | ||
2. importing `sagas.js` into the App container route configuration | ||
|
||
Like other sagas, your global saga will live within a container, the `App` container. Go ahead and add a file and use the following starter code; it's the same starter code for other sagas. | ||
|
||
######app/containers/App/sagas.js | ||
```JS | ||
import { take, call, put, select } from 'redux-saga/effects'; | ||
|
||
// Your sagas for this container | ||
export default [ | ||
sagaName, | ||
]; | ||
|
||
// Individual exports for testing | ||
export function* sagaName() { | ||
|
||
} | ||
``` | ||
|
||
Next we'll import the `sagas.js` file into our `App` route configuration by updating our `getComponent` function to asynchronosly load our files. You can just replace it with the one below. | ||
######app/routes.js | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
```JS | ||
export default function createRoutes(store) { | ||
// create reusable async injectors using getAsyncInjectors factory | ||
const { injectReducer, injectSagas } = getAsyncInjectors(store); | ||
|
||
childRoutes : [ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
// All your child routes are here inside this array | ||
]; | ||
|
||
return { | ||
getComponent(nextState, cb) { | ||
const importModules = Promise.all([ | ||
import('containers/App/sagas'), | ||
import('containers/App'), | ||
]); | ||
|
||
const renderRoute = loadModule(cb); | ||
|
||
importModules.then(([sagas, component]) => { | ||
injectSagas(sagas.default); | ||
renderRoute(component); | ||
}); | ||
|
||
importModules.catch(errorLoading); | ||
}, | ||
childRoutes, | ||
}; | ||
} | ||
``` |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
###### app/containers/App/sagas.js