We are currently warning about āunknown propertiesā in the DOM.
For example if you render:
<div foo="bar" />
youāre going to see a warning from React, and foo wonāt actually turn up in the DOM.
People have wanted support for custom attributes since forever: https://github.com/facebook/react/issues/140. Now that we've actually been warning for a whole release, I think it's a good time to flip this behavior, and to set any unknown attributes on the DOM instead of skipping them.
So the goal is that you would actually see <div foo="bar" /> in your DOM.
There is just one caveat: we still haven't updated all FB callsites to fix this warning. Ideally we want to stay synced with open source version of React, but I don't want React 16 to be delayed because of this, nor do I want delaying this change until React 17. So I think we should bite the bullet, introduce an internal feature flag that will differ for our FB builds, and enable the new behavior in the open source version. Some time during React 16 weāll finish updating our code, and remove the conditional code path.
I donāt think anybody on the team has time to work on this right now, so Iād love this to be a community contribution. Requirements as I see them:
ReactDOMFeatureFlags. Something like shouldSetCustomAttributes. Set it to true.shouldSetCustomAttributes is false. Make sure tests still cover this case (you can override feature flag in testsāsee existing tests concerning feature flags for how to do it).setAttribute for any unknown properties (rather than skipping them like we do now). Add tests for it. Those tests shouldnāt need to touch the feature flag (since itās the new default behavior). Make sure this works both for SVG and HTML.true) since we donāt use server rendering ourselves.Please let me know if youād like to take this. It could turn out a little complicated (there wonāt be a lot of guidance from us on this so we probably canāt coach a completely new contributor for this task). But if you sent a PR or two to React, you should be able to do it.
--
Please make sure to leave a comment here if you want to take it.
cc @aweary in case you were looking for something fun š
Hey @gaearon I would like to work on it.
I can give a hand too š
What's your take on onClick vs onclick for this?
I think it's a bad idea, it's pretty easy to let some props pass through without wanting too (onclick is a good example).
IMHO, a custom syntax (like a prop prefix) would be much better.
What's your take on onClick vs onclick for this?
I think we should still warn on casing typos. I can hardly imagine this ever being intentional.
I think the rendered html should have data-prop=, not prop= to comply to the w3c.
I think itās important that everyone commenting here also reads https://github.com/facebook/react/issues/140. There are plenty use cases for non-standard attributes, and the biggest motivation is enabling those + dropping the need for whitelist in production. Iām happy to debate the details of how we approach this, but I donāt want to turn this thread into a bikeshed on whether or not it is useful.
@islautin I donāt mean no disrespect, but I would really prefer that whoever jumps on this has contributed to React before. This is a fairly involved change, and shepherding PRs from new contributors takes a lot of team time (which we currently are running short of). We use this label for issues more appropriate for first-time contributors.
Update: @nhunzaker already had a PR for this in https://github.com/facebook/react/pull/7311, but it got stale.
@nhunzaker How much time do you think itāll take for you to circle back to that PR? If you have other outstanding PRs, which do you feel are blocking you from working on it?
The scroll-jank PR (https://github.com/facebook/react/pull/9333) just needs a bit more testing and approval. I was planning to write some DOM fixtures for media elements, but that could wait.
The DOM Factories PR (https://github.com/facebook/react/pull/8356) is complete work, it just needs to be integrated into the new build system on master and the old build system on 15.6-dev
If we want to get this in as fast as possible, I could definitely switch gears.
Misspellings will no longer be caught. Perhaps disabled by default, since this is an edge case for most.
Misspellings will no longer be caught
I donāt see why they wouldnāt (we can still warn for misspelling on things React is aware of, like onClick). Please see discussion above.
The scroll-jank PR (#9333) just needs a bit more testing and approval. I was planning to write some DOM fixtures for media elements, but that could wait.
Perhaps @spicyj could take over it?
The DOM Factories PR (#8356) is complete work, it just needs to be integrated into the new build system on master and the old build system on 15.6-dev
Perhaps @flarnie could take over it?
No pressure to either of you š Just noticed it seems like most of the work is done, and itās now at approval / cherry-picking stage where it would make for the team to jump on them.
@gaearon & @nhunzaker I can take over integrating the DOM Factories PR with the old/new build systems - that way @nhunzaker will be free to either focus on #9333 or help with this issue. Thanks for asking. :)
Sounds good, thanks @flarnie!
This might affect server rendering test suite previously added by @aickin. You might need to change those tests to verify the new behavior. Itās fine to only verify the new behavior there (with flag set to true) since we donāt use server rendering ourselves.
As a pointer to implementors, I think the tests that will be affected are all in the "unknown attributes" suite here: https://github.com/facebook/react/blob/master/src/renderers/dom/shared/__tests__/ReactDOMServerIntegration-test.js#L434
I think they are pretty self explanatory, but if not, feel free to ping me!
Also, one weird quirk occurs to me: with this patch, I think you still won't be able to add attributes with the names className or htmlFor to the rendered HTML. That's almost certainly fine, but perhaps worth noting.
We use this label for issues more appropriate for first-time contributors.
@gaearon, the first 5 issues in that list have already been taken by someone and in some cases a PR submitted. That's a lot of reading and digging to come up empty handed. Is there a more efficient way of finding issues that no one has claimed yet?
Noāthere arenāt that many bugs in React, and we also donāt add a ton of features. So these quickly get taken, and most contribution opportunities are in ecosystem libraries, and related tooling. And of course we appreciate people helping provide support to other people (who make honest mistakes) both in issues and on StackOverflow and chats.
(As a side note I want to slightly change the label system for claimed issues, but thatās a separate discussion. Please file a new issue and Iāll reply to you there.)
@gaearon I agree that this is really useful, I meant that you could automatically append data- before any custom attributes, so <Element foo="bar"> turns into <Element data-foo="bar"> unless it's an official attribute like href, name, etc ofcourse.
We have a specific proposal in https://github.com/facebook/react/issues/10399 and a PR attempting to implement it in https://github.com/facebook/react/pull/10385.
Iāll close thisāletās follow up there.
Most helpful comment
I think itās important that everyone commenting here also reads https://github.com/facebook/react/issues/140. There are plenty use cases for non-standard attributes, and the biggest motivation is enabling those + dropping the need for whitelist in production. Iām happy to debate the details of how we approach this, but I donāt want to turn this thread into a bikeshed on whether or not it is useful.