Skip to content
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 1 commit into from
Aug 7, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
112 changes: 112 additions & 0 deletions docs/docs/guides/path-parameters.mdx
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.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1


```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