-
Notifications
You must be signed in to change notification settings - Fork 48.6k
this.key seems to always be undefined inside a React component #2429
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
Comments
By design I believe, @sebmarkbage |
Yea, this is by design. We had a note about it in the change log: http://facebook.github.io/react/blog/#changelog The reason is that the concept of a key is something that is controlled by React internals before your component gets created. The same thing for refs. You can think about an array of A Map is a series of If you're reading a prop named This change makes the concept a bit more strict. This helps avoids bugs with transferring props to another component which shouldn't bring the key and ref along with it. It also helps performance by ensure that types in React internals are consistent and stable. I would suggest renaming or duplicating the prop name as a possible fix if you really need to access it. |
Thanks @sebmarkbage! I really appreciate the response. I kept reading the change log related to this, but I still couldn't figure out what it meant when it said Anyway, I'll add a new prop, thanks again for the response. |
@willdady So to address what seems to be the confusion; |
Thanks for the extra info @syranide – that makes sense as you explained it. Thanks! |
+1for @willdady, I also tried this.key and had to google until I found an explanation/solution here: https://groups.google.com/d/msg/reactjs/MW2wIBi-pMg/ZrXwUdZUEa4J Please clarify the blog post to prevent others from the headaches ;) |
I would very much like to have access to the I use that attribute as a way of accessing a model's key attribute. I recognize that you are suggesting the ref value should be semantically different than what I am proposing but I don't understand why this change is being forced. This is meaningful to me for use with https://github.com/jhudson8/react-backbone Please, any way of accessing the ref going forward would be meaningful - even if there is a separate utility method that would return this value of a component is passed as a parameter. Thank you. |
@jhudson8 I think for now |
@HPCodecraft That's already gone in master. @jhudson8 It's likely that ref will change from a string to a richer data type which allow you to share a reference between siblings. See #1373 Could something like that work? |
Well, I think ultimately I just need to forget about what I am wanting because I'm not going to stop this train. And, I do see the advantages of not using string references - it just made things much more convenient. Thanks for your time. |
@jhudson8 We take breaking changes very seriously. We want to make sure that it's always at least possible to upgrade every use case... and ideally make it somewhat convenient. I think that either the first-class refs or context will be able to help you solve the use case though. :) Feel free to ping me if you have trouble solving the upgrade path. |
Key should be accessible via a getter. There are many instances when one would need to know which unique child in an array of children something happened to. A simple example would be a list of items that get
If key isn't accessible, now we need to pass another arbitrary prop, like |
@epferrari You could always bind |
@mathieumg yeah, but man does React complain when you bind a component method to the component! I suppose one could use a utility function to curry |
React shouldn't complain if you provide a second argument. E.g. |
How about that... never tried with an argument, just scoffed at the warning and moved on. Thanks for the tip! |
TLDR: |
@caseys lol that's so true. So, we can't access the keys? why not to provide a getter, is that hard? I have an use case where accesing the keys are useful: jsx-to-string: It's a npm module to transform from jsx to string. If you use this and a syntax highlight you can create a react component that displays the code of his children, useful for examples, but not that useful if you want to display code using keys. If you pass a component with keys, you can't see the key. GREAT! ok no, it's not. So yeah, react wants you to add keys to your components, but at the same time, it deny the access to it. If keys are react internals, then they shouldn't be available to the user in the first place. |
Your conclusion is wrong. The key is not available from inside the component itself, but it is readily available from the element (which is the use case for By the way, we officially maintain and recommend to use pretty-format with |
@erasmo-marin I'm curious what your use-case is . |
Thinking in a future maintenance of my app, in cases that I need to write a custom key, how can I write an automated test that testing if exists and value of the key? is it important to test this scenario? |
* make delete and cancel only use existing id * had to not use `key`: * see facebook/react#2429
Hello, I have use cases. Let me get this out of the way: I'm against the notion that the key prop should only be meant for React's under-the-hood rendering algorithm. The fact of the matter is, it's a unique key that I give it and can have many valuable uses if used responsibly. usecase1) usecase 2 Conclusion |
@learis Think of if it (and it literally is), the key in a Map. If you get the value for a given key and pass it to a function, then all that function will have access to the is value unless otherwise explicitly passed on as part of the API. The purpose of key here though is only to facilitate identity and may even contain "sensitive" information, and is not intended for the child to access. So while it may be convenient to access the key, it is not sanitary. It's much better to explicitly make it part of your component, even though it may seem redundant. |
This wouldn't actually give you unique IDs because they would be only unique amongst siblings. So only one level up. So this would give you a false sense of assurance. We do have something in the works for this use case but it's not related to keys: #17322.
I'm not sure which use case this is solving. This sounds like a description of a solution, not a problem.
It's not necessarily "misuse". It's that this doesn't scale to larger teams. We've tried that already — it used to be on the props. We moved it out. The reason it doesn't work is the following:
Since most likely eventually you'll want to separate them anyway, we force you to do it early before it becomes a problem. |
@gaearon |
I think "ignorance" implies people lack the knowledge, and if they were taught properly, everything would be great. But what I'm talking about is locality. Generally, we want to be able to make a change to a component without having to make sweeping changes across its dependency graph. Putting key in props violates locality because it makes it no longer safe to change |
@gaearon But this is practically the only situation I can think of where key is used elsewhere for what it truly represents (which you mentioned earlier): an id that is unique among its sibling components. And as you have shown, this locality issue can spiral into a huge problem. Thanks again for the patience in explaining it to me. |
Ironically we did have something like this as an implementation detail before React 15. Each DOM node had a Curious how would you use such an ID? Generally speaking, if you're debugging where an element comes from, React DevTools helps a lot. |
I have just finished schooling and am making my first websites from the ground up. This was just an idea that popped into my head that seemed like it could be very useful. I have little experience going deep into debugging so it very well may be that this idea is unneeded even though it sounds really useful to me in concept. In the future, if it proves to be useful in a way that can't be accomplished through DevTools or other things, I'll bring it up. Take care and happy coding. |
For future reference, if you are creating a set of JSX elements with |
In React 0.12.0 if you attempt to use this.key inside a react component, it is
undefined
Simple test case here: http://jsfiddle.net/jg2pd4ob/2/
In 0.11.x you could access this value via
this.props.key
which has been removed.The text was updated successfully, but these errors were encountered: