React報錯Type '() => JSX.Element[]' is not assignable to type FunctionComponent
總覽
當我們嘗試從函數(shù)組件中返回元素組成的數(shù)組時,會產(chǎn)生"Type '() => JSX.Element[]' is not assignable to type FunctionComponent"錯誤。為了解決該錯誤,可以將元素數(shù)組包裹在React片段中。

這里有個示例用來展示錯誤是如何發(fā)生的。
// App.tsx
import React from 'react';
// ?? Type '() => JSX.Element[]' is not assignable to type 'FunctionComponent<{}>'.
// Type 'Element[]' is missing the following properties
// from type 'ReactElement<any, any>': type, props, key ts(2322)
const App: React.FunctionComponent = () => {
return ['Alice', 'Bob'].map(element => <div key={element}>{element}</div>);
};
export default App;
這是完全有效的React.js代碼,因為我們能夠從React的函數(shù)組件中返回一個數(shù)組。然而,FunctionComponent接口的返回類型是ReactElement或null。
這也就意味著,我們可以只返回一個React元素或者null值。
React片段
為了解決該類型錯誤,我們必須將數(shù)組包裹在React片段(React fragment)中。
// App.tsx
import React from 'react';
const App: React.FunctionComponent = () => {
return (
<>
{['Alice', 'Bob'].map(element => (
<div key={element}>{element}</div>
))}
</>
);
};
export default App;
當我們需要對一個元素列表進行分組而不向DOM添加額外的節(jié)點時,就會用到片段。
React.Fragment
你可能還會看到使用了更加詳細的片段語法。
// App.tsx
import React from 'react';
const App: React.FunctionComponent = () => {
return (
<React.Fragment>
{['Alice', 'Bob'].map(element => (
<div key={element}>{element}</div>
))}
</React.Fragment>
);
};
export default App;
上面的兩個例子達到了相同的結(jié)果--它們對元素列表的元素進行分組,而沒有給DOM添加額外的節(jié)點。
div
另一個解決方案是將元素數(shù)組包裹在另一個DOM元素中,例如一個div。
// App.tsx
import React from 'react';
const App: React.FunctionComponent = () => {
return (
<div>
{['Alice', 'Bob'].map(element => (
<div key={element}>{element}</div>
))}
</div>
);
};
export default App;
這仍然符合FunctionComponent接口中指定的返回類型,因為我們的組件返回的是一個單一的React元素。
總結(jié)
為了解決"Type '() => JSX.Element[]' is not assignable to type FunctionComponent"錯誤,可以使用React片段或者div將元素數(shù)組進行包裹。
以上就是React報錯Type '() => JSX.Element[]' is not assignable to type FunctionComponent的詳細內(nèi)容,更多關(guān)于React報錯FunctionComponent的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
詳解關(guān)于react-redux中的connect用法介紹及原理解析
本篇文章主要介紹了詳解關(guān)于react-redux中的connect用法介紹及原理解析,非常具有實用價值,需要的朋友可以參考下2017-09-09
react使用節(jié)流函數(shù)防止重復(fù)點擊問題
這篇文章主要介紹了react使用節(jié)流函數(shù)防止重復(fù)點擊問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-06-06
React 如何使用時間戳計算得到開始和結(jié)束時間戳
這篇文章主要介紹了React 如何拿時間戳計算得到開始和結(jié)束時間戳,本文通過示例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-09-09
React useContext與useReducer函數(shù)組件使用
useContext和useReducer 可以用來減少層級使用, useContext,可以理解為供貨商提供一個公共的共享值,然后下面的消費者去接受共享值,只有一個供貨商,而有多個消費者,可以達到共享的狀態(tài)改變的目的2023-02-02
ahooks封裝cookie?localStorage?sessionStorage方法
這篇文章主要為大家介紹了ahooks封裝cookie?localStorage?sessionStorage的方法示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-07-07
React Native可定制底板組件Magic Sheet使用示例
這篇文章主要為大家介紹了React Native可定制的底板組件Magic Sheet使用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-10-10
react.js組件實現(xiàn)拖拽復(fù)制和可排序的示例代碼
這篇文章主要介紹了react.js組件實現(xiàn)拖拽復(fù)制和可排序的示例代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-08-08
React Fragment 和空標簽(<></>)用法及區(qū)別詳解
本文詳細介紹了React中的Fragment和空標簽的使用,包括它們的區(qū)別、使用場景、性能考慮以及最佳實踐,本文給大家介紹的非常詳細,感興趣的朋友跟隨小編一起看看吧2025-01-01

