We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
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
react发现个有趣的问题,在下面四种写法中,只有第一种会渲染demo
显示demo
const Demo = (props) => <div {...props} class="demo" />; <Demo>demo</Demo>
显示hello
const Demo = (props) => <div children="hello" class="demo" />; <Demo>demo</Demo>
不显示任何信息
const Demo = (props) => <div class="demo" />; <Demo>demo</Demo>
const Demo = () => <div class="demo" />; <Demo>demo</Demo>
why?
发生上面的原因是因为createElement函数
createElement
function createElement(type, config, children) { var propName = void 0; // Reserved names are extracted var props = {}; var key = null; var ref = null; var self = null; var source = null; if (config != null) { if (hasValidRef(config)) { ref = config.ref; } if (hasValidKey(config)) { key = '' + config.key; } self = config.__self === undefined ? null : config.__self; source = config.__source === undefined ? null : config.__source; // Remaining properties are added to a new props object for (propName in config) { if (hasOwnProperty.call(config, propName) && !RESERVED_PROPS.hasOwnProperty(propName)) { // 这里会把parent的children复制过来 props[propName] = config[propName]; } } } // 这里如果自身有children会覆盖来自parent的 var childrenLength = arguments.length - 2; if (childrenLength === 1) { props.children = children; } else if (childrenLength > 1) { var childArray = Array(childrenLength); for (var i = 0; i < childrenLength; i++) { childArray[i] = arguments[i + 2]; } { if (Object.freeze) { Object.freeze(childArray); } } props.children = childArray; } if (type && type.defaultProps) { var defaultProps = type.defaultProps; for (propName in defaultProps) { if (props[propName] === undefined) { props[propName] = defaultProps[propName]; } } } { if (key || ref) { var displayName = typeof type === 'function' ? type.displayName || type.name || 'Unknown' : type; if (key) { defineKeyPropWarningGetter(props, displayName); } if (ref) { defineRefPropWarningGetter(props, displayName); } } } return ReactElement(type, key, ref, self, source, ReactCurrentOwner.current, props); }
The text was updated successfully, but these errors were encountered:
No branches or pull requests
react发现个有趣的问题,在下面四种写法中,只有第一种会渲染demo
显示demo
显示hello
不显示任何信息
不显示任何信息
why?
发生上面的原因是因为
createElement
函数The text was updated successfully, but these errors were encountered: