React: Props shortcut in HTML/JSX writting

Created on 22 Jan 2019  路  3Comments  路  Source: facebook/react

Do you want to request a feature or report a bug?

Hello, I would like to requets a feature that has to do with the way the HTML part of the JSX is written.

What is the current behavior?

Today, to pass a prop that has the same name as the variable that carries the information, here is the behavior:

<Forum subjects={subjects} />

What is the expected behavior?

I (but not solely I, I'm sure) would love to be able to write this:

<Forum subjects />

I know that HTML allows this for booleans, but as JSX is neither HTML nor JS, I'm sure we can work something out to allow this for objects, the same way it's done in ES6:

console.log({ nameOfTheVariable })

for instance.

Most helpful comment

JSX also treats attributes with no value as booleans like:

<Forum subjects />

Is the same as:

<Forum subjects={true} />

Maybe surrounding the attribute name with braces would be a good way to differentiate the behaviour and allow what you are asking for to work without conflicts:

<Forum {subjects} />

To be a syntatic sugar for:

<Forum subjects={subjects} />

All 3 comments

JSX also treats attributes with no value as booleans like:

<Forum subjects />

Is the same as:

<Forum subjects={true} />

Maybe surrounding the attribute name with braces would be a good way to differentiate the behaviour and allow what you are asking for to work without conflicts:

<Forum {subjects} />

To be a syntatic sugar for:

<Forum subjects={subjects} />

Definitely, that'd be great !

JSX isn't part of React itself and is used by other libraries as well. There is an official specification and repo for JSX. I recommend you refer to those resources, as there is already an open issue for this. Thanks!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

gaearon picture gaearon  路  126Comments

addyosmani picture addyosmani  路  143Comments

gaearon picture gaearon  路  133Comments

iammerrick picture iammerrick  路  94Comments

gaearon picture gaearon  路  104Comments