-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix props rendered as attributes (#61)
* API refactor for avoiding rendering props as DOM attributes * Readme updates * Upgrade deps & docs * Update docs * Update docs
- Loading branch information
Showing
6 changed files
with
959 additions
and
959 deletions.
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 |
---|---|---|
@@ -1,52 +1,28 @@ | ||
## Migrating from 1.x.x -> 2.x.x | ||
## Migrating from 2.x.x -> 3.x.x | ||
|
||
#### 1.) `withBreadcrumbs` is now the default export | ||
#### First things, first... | ||
|
||
**1.x.x** | ||
```js | ||
import { withBreadcrumbs } from 'react-router-breadcrumbs-hoc'; | ||
``` | ||
`withBreadcrumbs` now returns an array of `Object`s instead of `Component`s: | ||
|
||
**2.x.x** | ||
```js | ||
import withBreadcrumbs from 'react-router-breadcrumbs-hoc'; | ||
```diff | ||
- breadcrumbs.map(breadcrumb) | ||
+ breadcrumbs.map({ breadcrumb }) | ||
``` | ||
|
||
#### 2.) The breadcrumbs array returned by the HOC is now _just_ the components. It _used_ to be an array of objects, but I decided this approach was easier to understand and made the implementation code a bit cleaner. | ||
|
||
**1.x.x** | ||
```js | ||
{breadcrumbs.map(({ breadcrumb, path, match }) => ( | ||
<span key={path}> | ||
<NavLink to={match.url}> | ||
{breadcrumb} | ||
</NavLink> | ||
</span> | ||
))} | ||
``` | ||
Within this object, other props like `match`, `location`, and pass-through props are also returned: | ||
|
||
**2.x.x** | ||
```js | ||
{breadcrumbs.map(breadcrumb => ( | ||
<span key={breadcrumb.props.key}> | ||
<NavLink to={breadcrumb.props.match.url}> | ||
{breadcrumb} | ||
</NavLink> | ||
</span> | ||
))} | ||
```diff | ||
- breadcrumbs.map((breadcrumb) => {}) | ||
+ breadcrumbs.map(({ breadcrumb, match, location, someCustomProp }) => {}) | ||
``` | ||
|
||
#### 3.) The package will now attempt to return sensible defaults for breadcrumbs unless otherwise provided making the the package now "opt-out" instead of "opt-in" for all paths. See the readme for how to disable default breadcrumb behavior. | ||
#### Why was this change made? | ||
|
||
**1.x.x** | ||
```js | ||
withBreadcrumbs([ | ||
{ path: '/', breadcrumb: 'Home' }, | ||
{ path: '/users', breadcrumb: 'Users' }, | ||
])(Component); | ||
``` | ||
Under the hood, `withBreadcrumbs` uses React's `createElement` method to render breadcrumbs. In version 2, all props (like `match`, `location`, etc) were assigned to the rendered component (for example: `createElement(breadcrumb, componentProps);`). | ||
|
||
**2.x.x** (the above breadcrumbs will be automagically generated so there's no need to include them in config) | ||
```js | ||
withBreadcrumbs()(Component); | ||
``` | ||
This had the unintended side-effect of rendering any of these props as an _attribute_ on the DOM element. So, ultimately this resulted in some breadcrumbs rendering like `<span someProp="[Object object]"/>'` as well as some React console warnings [in certain cases](https://github.com/icd2k3/react-router-breadcrumbs-hoc/issues/59). | ||
|
||
This issue has been solved by adding the following logic: | ||
- If the breadcrumb is a simple string, don't render it with props applied | ||
- If the breadcrumb is a function/class (dynamic), _then_ pass all the props to it | ||
- Return objects instead of components so that we can still utilize all the props during the `map` |
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
Oops, something went wrong.