React?組件傳?children?的各種案例方案詳解
自定義組件的時候往往需要傳 children
,由于寫法比較多樣,我就總結(jié)了一下。
要自定義的組件是這樣的:
其中包含一個 title
和一個 children
。
定義一個后面要用到的 Props
:
/** 定義屬性對象 * - title: 標題 * - 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)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
React使用Echarts/Ant-design-charts的案例代碼
這篇文章主要介紹了React使用Echarts/Ant-design-charts的實例代碼,本文通過實例代碼給大家講解的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-11-11React之如何在Suspense中優(yōu)雅地請求數(shù)據(jù)
Suspense 是 React 中的一個組件,直譯過來有懸掛的意思,能夠?qū)⑵浒漠惒浇M件掛起,直到組件加載完成后再渲染,本文詳細介紹了如何在Suspense中請求數(shù)據(jù),感興趣的小伙伴可以參考閱讀本文2023-04-04解決React報錯Expected an assignment or funct
這篇文章主要為大家介紹了React報錯Expected an assignment or function call and instead saw an expression解決方案詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-12-12使用Axios在React中請求數(shù)據(jù)的方法詳解
這篇文章主要給大家介紹了初學React,如何規(guī)范的在react中請求數(shù)據(jù),主要介紹了使用axios進行簡單的數(shù)據(jù)獲取,加入狀態(tài)變量,優(yōu)化交互體驗,自定義hook進行數(shù)據(jù)獲取和使用useReducer改造請求,本文主要適合于剛接觸React的初學者以及不知道如何規(guī)范的在React中獲取數(shù)據(jù)的人2023-09-09