-
Notifications
You must be signed in to change notification settings - Fork 50
Add key to the element wrapped in the Route #40
base: master
Are you sure you want to change the base?
Conversation
Codecov Report
@@ Coverage Diff @@
## master #40 +/- ##
==========================================
+ Coverage 37.73% 41.07% +3.33%
==========================================
Files 6 6
Lines 53 56 +3
Branches 12 14 +2
==========================================
+ Hits 20 23 +3
Misses 24 24
Partials 9 9
Continue to review full report at Codecov.
|
package.json
Outdated
@@ -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\"", |
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.
@ForsakenHarmony Are you on windows? What's the purpose of this? Just curious.
location: location | ||
}) | ||
|
||
child.props.key = child.props.key || props.path |
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.
@ForsakenHarmony @zaceno Is this infallible?
Anyone familiar with ReactRouter, what's going on there?
I like this though.
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.
props
always gets added by h
at least, if that's what you're asking
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.
Nope, I mean does ReactRouter also add a key to the route component?
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.
afaik, no, but they have component methods, which don't depend on elements that might be reused
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.
@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 oncreate
won't get called for routed components. You have to manually key them to make it work. Beginners are required to understand the relationship between keys and lifecycle events. Higher threshold.
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.
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.
What about this?
var key =
props.key ||
props.path + (props.render ? props.render.name + props.render.length : "")
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.
Arrow functions have no name. 😞
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.
They do if you assign them to a variable, but anonymously used, they don't, just ""
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 guess we could use props.render.toString()
, but that feels asking for trouble.
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.
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 child.props.key = child.props.key
should be fine, but that too is subject to what we do with jorgebucaran/hyperapp#555.
/cc @frenzzy
"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" |
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.
Just leaving a comment here: https://facebook.github.io/jest/docs/en/configuration.html#testurl-string
Nice.
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 wanted to do it in the test, but couldn't figure out how to do that
@@ -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\"", |
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
Could you provide a description explaining what this PR does and what problem you are trying to solve? |
#38 |
Thanks! So just to clarify the desired behaviour; in this case:
The first route gets no key Is this correct? |
Yea, I guess so |
The path seems like a sensible key to use. It is a good identifier for the element. The only doubt I have; is it guaranteed to be unique? Does it even matter if it is not? If it does matter, how should we document it? Otherwise SGTM 👍 |
it was mostly to address an issue I had with element reuse and would be the same thing with non unique paths ig |
@lukejacksonn @ForsakenHarmony Zaceno explained it best here. |
Fixes #38