React?組件傳?children?的各種案例方案詳解
自定義組件的時(shí)候往往需要傳 children,由于寫法比較多樣,我就總結(jié)了一下。
要自定義的組件是這樣的:

其中包含一個(gè) title 和一個(gè) children。
定義一個(gè)后面要用到的 Props:
/** 定義屬性對(duì)象
* - title: 標(biāo)題
* - children: 子組件
*/
type Props = {
title: string;
children?: React.ReactNode;
};1. 類組件
1.1 類組件,不使用解構(gòu)
class ClassComponent1 extends Component<Props> {
render(): ReactNode {
return (
<div style={{ backgroundColor: 'red' }}>
<h2>{this.props.title}</h2>
{this.props.children}
</div>
);
}
}1.2 類組件,使用解構(gòu)
class ClassComponent2 extends Component<Props> {
render(): ReactNode {
// 解構(gòu)賦值
const { title, children } = this.props;
return (
<div style={{ backgroundColor: 'red' }}>
<h2>{title}</h2>
{children}
</div>
);
}
}2. 函數(shù)組件
2.1 函數(shù)組件,不使用解構(gòu)
const FunctionComponent1: React.FC<Props> = (props) => {
return (
<div style={{ backgroundColor: 'orange' }}>
<h2>{props.title}</h2>
{props.children}
</div>
);
};2.2 函數(shù)組件,外部解構(gòu)
const FunctionComponent2: React.FC<Props> = ({ title, children }) => {
return (
<div style={{ backgroundColor: 'orange' }}>
<h2>{title}</h2>
{children}
</div>
);
};2.3 函數(shù)組件,內(nèi)部解構(gòu)
const FunctionComponent3: React.FC<Props> = (props) => {
// 解構(gòu)賦值
const { title, children } = props;
return (
<div style={{ backgroundColor: 'orange' }}>
<h2>{title}</h2>
{children}
</div>
);
};3. 普通函數(shù)
3.1 普通函數(shù),內(nèi)部解構(gòu)
function NormalFunction1(props: Props) {
// 解構(gòu)賦值
const { title, children } = props;
return (
<div style={{ backgroundColor: 'yellow' }}>
<h2>{title}</h2>
{children}
</div>
);
}3.2 普通函數(shù),外部解構(gòu)
function NormalFunction2({ title, children }: Props) {
return (
<div style={{ backgroundColor: 'yellow' }}>
<h2>{title}</h2>
{children}
</div>
);
}3.3 普通函數(shù),外部解構(gòu),不使用自定義Type
function NormalFunction3({
title,
children,
}: {
title: string;
children?: React.ReactNode;
}) {
return (
<div style={{ backgroundColor: 'yellow' }}>
<h2>{title}</h2>
{children}
</div>
);
}3.4 普通函數(shù),不使用解構(gòu),不使用自定義Type
function NormalFunction4(props: { title: string; children?: React.ReactNode }) {
return (
<div style={{ backgroundColor: 'yellow' }}>
<h2>{props.title}</h2>
{props.children}
</div>
);
}調(diào)用及展示
export default class ChildrenPage extends Component {
render() {
return (
<div style={{ padding: '20px' }}>
<h1>組件傳children</h1>
<ClassComponent1 title="類組件,不使用解構(gòu)">
<p>這里是children</p>
</ClassComponent1>
<ClassComponent2 title="類組件,使用解構(gòu)">
<p>這里是children</p>
</ClassComponent2>
<FunctionComponent1 title="函數(shù)組件,不使用解構(gòu)">
<p>這是里children</p>
</FunctionComponent1>
<FunctionComponent2 title="函數(shù)組件,外部解構(gòu)">
<p>這是里children</p>
</FunctionComponent2>
<FunctionComponent3 title="函數(shù)組件,內(nèi)部解構(gòu)">
<p>這是里children</p>
</FunctionComponent3>
<NormalFunction1 title="普通函數(shù),內(nèi)部解構(gòu)">
<p>這里是children</p>
</NormalFunction1>
<NormalFunction2 title="普通函數(shù),外部解構(gòu)">
<p>這里是children</p>
</NormalFunction2>
<NormalFunction3 title="普通函數(shù),外部解構(gòu),不使用自定義Type">
<p>這里是children</p>
</NormalFunction3>
<NormalFunction4 title="普通函數(shù),不使用解構(gòu),不使用自定義Type">
<p>這里是children</p>
</NormalFunction4>
</div>
);
}
}
到此這篇關(guān)于React 組件傳 children 的各種方案的文章就介紹到這了,更多相關(guān)React 組件傳 children內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
React使用Echarts/Ant-design-charts的案例代碼
這篇文章主要介紹了React使用Echarts/Ant-design-charts的實(shí)例代碼,本文通過實(shí)例代碼給大家講解的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-11-11
React之如何在Suspense中優(yōu)雅地請(qǐng)求數(shù)據(jù)
Suspense 是 React 中的一個(gè)組件,直譯過來有懸掛的意思,能夠?qū)⑵浒漠惒浇M件掛起,直到組件加載完成后再渲染,本文詳細(xì)介紹了如何在Suspense中請(qǐng)求數(shù)據(jù),感興趣的小伙伴可以參考閱讀本文2023-04-04
react?diff?算法實(shí)現(xiàn)思路及原理解析
這篇文章主要介紹了react?diff?算法實(shí)現(xiàn)思路及原理解析,本節(jié)我們正式進(jìn)入基本面試必考的核心地帶?--?diff?算法,了解如何優(yōu)化和復(fù)用?dom?操作的,還有我們常見的?key?的作用,需要的朋友可以參考下2022-05-05
React中進(jìn)行條件渲染的實(shí)現(xiàn)方法
React是一種流行的JavaScript庫(kù),它被廣泛應(yīng)用于構(gòu)建Web應(yīng)用程序,在React中,條件渲染是一個(gè)非常重要的概念,它允許我們根據(jù)不同的條件來呈現(xiàn)不同的內(nèi)容,在本文中,我們將探討React如何進(jìn)行條件渲染,需要的朋友可以參考下2023-11-11
React Native項(xiàng)目中使用Lottie動(dòng)畫的方法
這篇文章主要介紹了React Native 實(shí)現(xiàn)Lottie動(dòng)畫的相關(guān)知識(shí),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-10-10
React性能優(yōu)化的實(shí)現(xiàn)方法詳解
react憑借virtual DOM和diff算法擁有高效的性能,除此之外也有很多其他的方法和技巧可以進(jìn)一步提升react性能,在本文中我將列舉出可有效提升react性能的幾種方法,幫助我們改進(jìn)react代碼,提升性能2023-01-01
解決React報(bào)錯(cuò)Expected an assignment or funct
這篇文章主要為大家介紹了React報(bào)錯(cuò)Expected an assignment or function call and instead saw an expression解決方案詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-12-12
使用Axios在React中請(qǐng)求數(shù)據(jù)的方法詳解
這篇文章主要給大家介紹了初學(xué)React,如何規(guī)范的在react中請(qǐng)求數(shù)據(jù),主要介紹了使用axios進(jìn)行簡(jiǎn)單的數(shù)據(jù)獲取,加入狀態(tài)變量,優(yōu)化交互體驗(yàn),自定義hook進(jìn)行數(shù)據(jù)獲取和使用useReducer改造請(qǐng)求,本文主要適合于剛接觸React的初學(xué)者以及不知道如何規(guī)范的在React中獲取數(shù)據(jù)的人2023-09-09

