MCPcopy
hub / github.com/facebook/react / ReactElement

Function ReactElement

packages/react/src/jsx/ReactJSXElement.js:161–274  ·  view source on GitHub ↗

* Factory method to create a new React element. This no longer adheres to * the class pattern, so do not use new to call it. Also, instanceof check * will not work. Instead test $$typeof field against Symbol.for('react.transitional.element') to check * if something is a React Element. * * @inte

(type, key, props, owner, debugStack, debugTask)

Source from the content-addressed store, hash-verified

159 * @internal
160 */
161function ReactElement(type, key, props, owner, debugStack, debugTask) {
162 // Ignore whatever was passed as the ref argument and treat `props.ref` as
163 // the source of truth. The only thing we use this for is `element.ref`,
164 // which will log a deprecation warning on access. In the next release, we
165 // can remove `element.ref` as well as the `ref` argument.
166 const refProp = props.ref;
167
168 // An undefined `element.ref` is coerced to `null` for
169 // backwards compatibility.
170 const ref = refProp !== undefined ? refProp : null;
171
172 let element;
173 if (__DEV__) {
174 // In dev, make `ref` a non-enumerable property with a warning. It's non-
175 // enumerable so that test matchers and serializers don't access it and
176 // trigger the warning.
177 //
178 // `ref` will be removed from the element completely in a future release.
179 element = {
180 // This tag allows us to uniquely identify this as a React Element
181 $$typeof: REACT_ELEMENT_TYPE,
182
183 // Built-in properties that belong on the element
184 type,
185 key,
186
187 props,
188
189 // Record the component responsible for creating this element.
190 _owner: owner,
191 };
192 if (ref !== null) {
193 Object.defineProperty(element, 'ref', {
194 enumerable: false,
195 get: elementRefGetterWithDeprecationWarning,
196 });
197 } else {
198 // Don't warn on access if a ref is not given. This reduces false
199 // positives in cases where a test serializer uses
200 // getOwnPropertyDescriptors to compare objects, like Jest does, which is
201 // a problem because it bypasses non-enumerability.
202 //
203 // So unfortunately this will trigger a false positive warning in Jest
204 // when the diff is printed:
205 //
206 // expect(<div ref={ref} />).toEqual(<span ref={ref} />);
207 //
208 // A bit sketchy, but this is what we've done for the `props.key` and
209 // `props.ref` accessors for years, which implies it will be good enough
210 // for `element.ref`, too. Let's see if anyone complains.
211 Object.defineProperty(element, 'ref', {
212 enumerable: false,
213 value: null,
214 });
215 }
216 } else {
217 // In prod, `ref` is a regular property and _owner doesn't exist.
218 element = {

Callers 5

jsxProdFunction · 0.70
jsxDEVImplFunction · 0.70
createElementFunction · 0.70
cloneAndReplaceKeyFunction · 0.70
cloneElementFunction · 0.70

Calls 1

freezeMethod · 0.80

Tested by

no test coverage detected