-
Notifications
You must be signed in to change notification settings - Fork 48
Add key to the element wrapped in the Route #40
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -24,12 +24,15 @@ | |
| "bundle": "rollup -i src/index.js -o dist/router.js -f umd -mn router -g hyperapp:hyperapp", | ||
| "minify": "uglifyjs dist/router.js -o dist/router.js -mc pure_funcs=Object.defineProperty --source-map includeSources,url=router.js.map", | ||
| "prepublish": "npm run build", | ||
| "format": "prettier --semi false --write 'src/**/*.js'", | ||
| "format": "prettier --semi false --write \"src/**/*.js\" \"test/**/*.js\"", | ||
| "release": "npm run build && npm test && git commit -am $npm_package_version && git tag $npm_package_version && git push && git push --tags && npm publish" | ||
| }, | ||
| "babel": { | ||
| "presets": "env" | ||
| }, | ||
| "jest": { | ||
| "testURL": "http://test/test" | ||
|
Owner
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. Just leaving a comment here: https://facebook.github.io/jest/docs/en/configuration.html#testurl-string Nice.
Author
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. I wanted to do it in the test, but couldn't figure out how to do that |
||
| }, | ||
| "devDependencies": { | ||
| "babel-preset-env": "^1.6.1", | ||
| "hyperapp": "0.18.0", | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,11 +6,14 @@ export function Route(props) { | |
| exact: !props.parent | ||
| }) | ||
|
|
||
| return ( | ||
| match && | ||
| props.render({ | ||
| match: match, | ||
| location: location | ||
| }) | ||
| ) | ||
| if (!match) return | ||
|
|
||
| var child = props.render({ | ||
| match: match, | ||
| location: location | ||
| }) | ||
|
|
||
| child.props.key = child.props.key || props.path | ||
|
Owner
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. @ForsakenHarmony @zaceno Is this infallible? Anyone familiar with ReactRouter, what's going on there? I like this though.
Author
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.
Owner
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. Nope, I mean does ReactRouter also add a key to the route component?
Author
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. afaik, no, but they have component methods, which don't depend on elements that might be reused
Contributor
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. @jorgebucaran is it infallible? ...well... this: <main>
<Route path="/show/secrets" render={SecretComponent} />
<AComponent />
<AnotherComponent />
<Route path="/show/secrets" render={AnotherSecretComponent} />
<ThirdComponent />
</main>I e using the route as a switch to turn on or off multiple sibling components. Then the siblings will be keyed with the same key which of course is no good. ...unless the components have their own keys, in which case those keys will be used instead. So: A - Before: By default B - This proposal: You get automatic keys which make sense for the most part. And hence a lower threshold for beginners. But you run the risk of key-collisions and you don't understand why.
Owner
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. What about this? var key =
props.key ||
props.path + (props.render ? props.render.name + props.render.length : "")
Owner
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. Arrow functions have no name. 😞
Author
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. They do if you assign them to a variable, but anonymously used, they don't, just
Owner
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. I guess we could use
Owner
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. Keys have a pivotal role in the VDOM, and instead of hiding this them, we should ask users to properly key their routes instead. Only /cc @frenzzy |
||
|
|
||
| return child | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| import { h } from "hyperapp" | ||
| import { Route } from "../src" | ||
|
|
||
| test("autokey", () => { | ||
| const route = h(Route, { path: "/test", render: () => h("div", {}, "test") }) | ||
| expect(route.props.key).toBe("/test") | ||
| }) | ||
|
|
||
| test("don't replace keys", () => { | ||
| const route = h(Route, { | ||
| path: "/test", | ||
| render: () => h("div", { key: "wow" }, "test") | ||
| }) | ||
| expect(route.props.key).toBe("wow") | ||
| }) |
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.
I used normal quotes, because single quotes seemed to break it on windows