[英]Returning an array from a JSX function and destructuring it elsewhere
Experimenting a bit with JSX at the moment and came across this idea :目前对 JSX 进行了一些试验,并发现了这个想法:
I can do this:我可以做这个:
import React from 'react'
const Test = () => {
return <div>Test</div>
}
export default Test
and then import this file somewhere else and do:然后在其他地方导入这个文件并执行:
<Test />
Why doesn't the same work if I return arrays from my JSX files?如果我从 JSX 文件返回数组,为什么不一样?
import React from 'react'
const Test = () => {
let arr = [
<div>Test 1</div>,
<div>Test 2</div>,
]
return arr;
}
export default Test
and then:进而:
let [firstElement, secondElement] = Test();
return (
<div>
<firstElement />
</div>
)
In both cases, what I am trying to render is a pretty plain <div>
.在这两种情况下,我试图渲染的是一个非常简单的
<div>
。 In the first case it's returned directly, in the second case it's destructured from an array.在第一种情况下,它直接返回,在第二种情况下,它是从数组中解构出来的。 However, the second case doesn't work for me?
但是,第二种情况对我不起作用? Should it?
应该是?
PS: I know I could do {firstElement}
- but why not <firstElement />
? PS:我知道我可以做
{firstElement}
- 但为什么不做<firstElement />
?
The following should work:以下应该工作:
let arr = [
() => <div>Test 1</div>,
() => <div>Test 2</div>,
]
What you're doing currently is calling React.createElement
twice.您目前正在做的是两次调用
React.createElement
。
First you call it inside the array ( <div>Test 1</div
), then you return the already created element from the function, and then you render it again, by calling <firstElement/>
.首先在数组内调用它 (
<div>Test 1</div
),然后从函数返回已经创建的元素,然后通过调用<firstElement/>
再次渲染它。
Side note, firstElement
should be uppercase, or else ( I think ) the JSX transformer will consider it a native DOM element, rather than your custom component, which will result in a throw later).旁注,
firstElement
应该是大写的,否则(我认为)JSX 转换器会将其视为本机 DOM 元素,而不是您的自定义组件,这将导致稍后抛出)。
Therefore either change what's in the array into components, as stated above ( () => <div>Test 1</div>
), or change <firstElement />
into {firstElement}
因此,要么将数组中的内容更改为组件,如上所述(
() => <div>Test 1</div>
),要么将<firstElement />
更改为{firstElement}
Either way, what you're doing is quite hacky and shouldn't be anything more than a curiosity.无论哪种方式,你正在做的事情都非常古怪,不应该只是一种好奇心。
Test
should be coded as: Test
应编码为:
const Test = () => {
const arr = [
<div>Test 1</div>,
<div>Test 2</div>,
];
return <>{arr}</>;
}
The two divs are already react elements, an object created by React, and not components.这两个 div 已经是 React 元素,是 React 创建的对象,而不是组件。 To render them use an expression (curly braces):
要呈现它们,请使用表达式(花括号):
const test = () => [ <div>Test 1</div>, <div>Test 2</div>, ] const [firstElement, secondElement] = test(); const Demo = () => ( <div> {firstElement} </div> ) ReactDOM.render( <Demo />, root )
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script> <div id="root"></div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.