-
Notifications
You must be signed in to change notification settings - Fork 562
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
Skip second argument in React.createElement() #45
Comments
Here is user space solution: const h = React.createElement;
React.createElement = (type, props, ...children) => {
if (typeof props !== 'object') {
children = [props, ...children];
props = {};
}
return h(type, props, ...children);
}; |
There's a library directly linked to by the React documentation that offers the syntax you're looking for. |
@streamich Thank you for your code:
@dantman hyperscript needs the children to be in an array, createElement doesn't, so using it actually adds a bit more overhead, that's why I'm looking to use createElement itself, or is it easy to turn off arrays in hyperscript somehow? |
@Superpencil Seems like a pretty small difference, but it does look like there is an issue on the topic and there is a branch in the repo linked that implements the behaviour you want. I'd think it would be easier to convince the library to implement the behaviour you want than to get React to change how the API works for a use case that React.createElement isn't intended for. |
This has a possible security issue: if you pass a user-provided string as the first child of a component without props, the user may be able to misuse your API, allowing them to insert any props they’d like into the component. |
Do you want to request a feature or report a bug?
A feature
What is the current behavior?
React.createElement(component, props, children)
, whenprops
aren't needed, you still have to passnull
or{}
as a second argument.What is the proposal?
Being allowed to omit the second argument, allowing this:
Demo code?
I currently, very naively, use this:
The text was updated successfully, but these errors were encountered: