-
Notifications
You must be signed in to change notification settings - Fork 18
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
[CLOSES #418][DOCS] Path Parameters #420
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
# Path Parameters | ||
|
||
Misk-Web uses [React Router][react-router-docs] under the hood to handle routing. | ||
|
||
React Router implicitly inserts 3 important props into components that are rendered within a route (which will likely be all components that are in a Misk-Web tab). | ||
|
||
These three are (and are documented fully in the [React Router docs][react-router-docs]): | ||
- history | ||
- location | ||
- match: useful for parsing out path parameters | ||
|
||
## How to use Path Parameters | ||
|
||
Path parmeters are useful for a page that references a specific element of a set. | ||
|
||
For example, consider a component that is a list of articles has a link to edit each specific article. To support permalinking, the path for the edit article component includes the article ID as a path parameter. | ||
|
||
To set this up, you will need to add a route to `routes/index.tsx` in your tab with a `:` prefixed path parameter variable like `:articleId`. | ||
|
||
Note the ordering of routes matter, put the most specific route first so that it matches first. | ||
|
||
```TSX | ||
// routes/index.tsx | ||
<Switch> | ||
<Route path="/articles/edit/:articleId/" component={EditContainer} /> | ||
<Route path="/articles" component={ArticlesContainer} /> | ||
</Switch> | ||
|
||
``` | ||
|
||
Use the path parameter variable in your component as following. | ||
|
||
```TSX | ||
// containers/EditContainer.tsx | ||
import { Spinner } from "@blueprintjs/core" | ||
import { IRouterProvidedProps } from "@misk/simpleredux" | ||
|
||
export const EditContainer = (props: IRouterProvidedProps) => { | ||
const { articleId } = props.match && props.match.params | ||
if (articleId) { | ||
return ( | ||
<h1>Edit {articleId}</h1> | ||
<EditForm articleId={articleId} /> | ||
) | ||
} else { | ||
return <Spinner /> | ||
} | ||
} | ||
|
||
``` | ||
|
||
Note the use of the `IRouterProvidedProps` to get typed access to the `match` props. | ||
|
||
## IRouterProvidedProps | ||
|
||
`IRouterProvidedProps` is provided as part of the [`@misk/simpleredux` library][irouter-provided-props-code]. Since all of the props are provided optionally by React Router, all members are nullable null checking must be performed on use. | ||
|
||
```TS | ||
// @misk/simpleredux/utilities | ||
export interface IRouterProvidedProps { | ||
history?: History | ||
location?: Location | ||
match?: match | ||
} | ||
``` | ||
|
||
## Inclusion in Ducks | ||
|
||
You can include `IRouterProvidedProps` in the `ducks/index.ts` default provided props for your tab as follows. | ||
|
||
```TS | ||
// ducks/index.ts | ||
import { | ||
IDispatchSimpleForm, | ||
IDispatchSimpleNetwork, | ||
IRouterProvidedProps | ||
} from "@misk/simpleredux" | ||
|
||
// ... | ||
|
||
/** | ||
* Dispatcher | ||
*/ | ||
export interface IDispatchProps | ||
extends IDispatchPalette, | ||
IDispatchSimpleForm, | ||
IDispatchSimpleNetwork, | ||
IRouterProvidedProps { } | ||
|
||
// ... | ||
``` | ||
|
||
This makes for easy use of React Router provided props in Redux connected containers since they come in the already included `IDispatchProps`. | ||
|
||
```TSX | ||
// containers/TabContainer.tsx | ||
import { connect } from "react-redux" | ||
import { IDispatchProps, IState, mapDispatchToProps, mapStateToProps } from "../ducks" | ||
|
||
const TabContainer = (props: IDispatchProps & IState) => { | ||
// ... | ||
} | ||
|
||
export default connect( | ||
mapStateToProps, | ||
mapDispatchToProps | ||
)(TabContainer) | ||
``` | ||
|
||
[react-router-docs]: https://reacttraining.com/react-router/ | ||
[irouter-provided-props-code]: https://github.com/cashapp/misk-web/blob/9b31fbad9ebe36d582cec525fc51a200e270c9bf/packages/%40misk/simpleredux/src/utilities.ts#L31-L41 | ||
|
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.
+1