欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

React利用props的children實(shí)現(xiàn)插槽功能

 更新時(shí)間:2023年07月02日 11:18:05   作者:-耿瑞-  
React中并沒有vue中的?slot?插槽概念?不過?可以通過props.children?實(shí)現(xiàn)類似功能,本文為大家整理了實(shí)現(xiàn)的具體方,需要的可以參考一下

可能會比較遺憾的說 React中并沒有vue中的 slot 插槽概念 不過 可以通過props.children 實(shí)現(xiàn)類似功能

我們先創(chuàng)建一個(gè)React項(xiàng)目

在src下創(chuàng)建目錄components 在下面創(chuàng)建一個(gè)dom.jsx組件

參考代碼如下

import React from "react"
export default class dom extends React.Component {
    constructor(props){
      super(props);
      this.state = {
      }
    }
    render(){
      return (
        <div>
           <div>這里是dom組件</div>
           <div>{ this.props.children }</div>
           <div>元素結(jié)束</div>
        </div>
      )
    }
}

這里 大家可以將this.props.children 理解為我們vue中的slot父組件插入的內(nèi)容就會放在這個(gè)位置

我們 App根組件編寫代碼如下

import React from "react"
import Dom from "./components/dom"
export default class App extends React.Component {
    constructor(props){
      super(props);
      this.state = {
      }
    }
    render(){
      return (
        <div>
           <Dom>
              <div>這是插槽內(nèi)容</div>
           </Dom>
        </div>
      )
    }
}

我們正常調(diào)用了剛才寫的dom組件 在中間插入了一個(gè)div 內(nèi)容為 這是插槽內(nèi)容

我們運(yùn)行結(jié)果如下

可以看到 我們的內(nèi)容被成功插入在了 this.props.children 的位置

知識補(bǔ)充

可能有小伙伴對于props.children并不太熟悉,下面小編為大家整理了props.children的相關(guān)知識,希望對大家有所幫助

簡介

在典型的React數(shù)據(jù)流模型中,props 是組件對外的接口。props 作為父子組件溝通的橋梁,為組件的通信和傳值提供了重要手段。

this.props 對象的屬性與組件的屬性一一對應(yīng),但其中有一個(gè)比較特殊的參數(shù):this.props.children。它表示組件所有的子節(jié)點(diǎn)。

在組件內(nèi)部使用 this.props.children,可以拿到用戶在組件里面放置的內(nèi)容。

如下例,一個(gè) span 標(biāo)簽在 Parent 中作為Child的子節(jié)點(diǎn)傳入,可在 Child 中通過 this.props.children 取到:

class Parent extends React.Component {
  render() {
    return (
    <Child>
        <span>{'child node'}</span>
      </Child>
    );
  }
}
class Child extends React.Component {
  render() {
    return (
      <div>
        {this.props.children}
      </div>
    );
  }
}

React.Children 方法

React 提供了工具方法 React.Children 來處理 this.props.children。

1. React.Children.map

object React.Children.map(object children, function fn)

遍歷 props.children ,在每一個(gè)子節(jié)點(diǎn)上調(diào)用 fn 函數(shù)。

2. React.Children.forEach

React.Children.forEach(object children, function fn)

類似于 React.Children.map(),但是不返回對象。

3. React.Children.count

number React.Children.count(object children)

返回 children 當(dāng)中的組件總數(shù)。

4. React.Children.only

object React.Children.only(object children)

返回 children 中僅有的子節(jié)點(diǎn)。如果在 props.children 傳入多個(gè)子節(jié)點(diǎn),將會拋出異常。

到此這篇關(guān)于React利用props的children實(shí)現(xiàn)插槽功能的文章就介紹到這了,更多相關(guān)React插槽內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • react中使用antd及immutable示例詳解

    react中使用antd及immutable示例詳解

    這篇文章主要為大家介紹了react中使用antd及immutable示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-08-08
  • React編程中需要注意的兩個(gè)錯(cuò)誤

    React編程中需要注意的兩個(gè)錯(cuò)誤

    React可以說是前端的先驅(qū)者,它總是會引領(lǐng)整個(gè)前端的潮流。 但我們在使用也經(jīng)常會遇到錯(cuò)誤,下面這篇文章主要給大家介紹了關(guān)于React編程中需要注意的兩個(gè)錯(cuò)誤,需要的朋友可以參考下
    2021-05-05
  • React Native 集成 iOS 原生功能(打印機(jī)功能為例)

    React Native 集成 iOS 原生功能(打印機(jī)功能為例)

    在 React Native 項(xiàng)目中集成 iOS 原生功能是一個(gè)常見需求,本文將同樣以打印機(jī)功能為例,詳細(xì)介紹如何在 React Native 項(xiàng)目中集成 iOS 原生功能,感興趣的朋友一起看看吧
    2024-12-12
  • react組件中debounce防抖功能失效問題解決辦法

    react組件中debounce防抖功能失效問題解決辦法

    在React組件中,如果使用useState管理searchKey,每次輸入變化都會導(dǎo)致組件重新渲染,從而生成新的debounce函數(shù),導(dǎo)致防抖功能失效,解決方法是使用useRef定義searchKey為非響應(yīng)式數(shù)據(jù),從而維持debounce函數(shù)的穩(wěn)定,確保防抖功能有效,感興趣的朋友跟隨小編一起看看吧
    2024-10-10
  • react入門級詳細(xì)筆記

    react入門級詳細(xì)筆記

    這篇文章講述了React的基本介紹,基本使用和React相關(guān)js庫.通過這篇文章可以入門React的使用,可以快速上手使用React進(jìn)行代碼的編寫
    2021-06-06
  • 詳解如何在React組件“外”使用父組件的Props

    詳解如何在React組件“外”使用父組件的Props

    這篇文章主要介紹了詳解如何在React組件“外”使用父組件的Props,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2018-01-01
  • react+antd實(shí)現(xiàn)動態(tài)編輯表格數(shù)據(jù)

    react+antd實(shí)現(xiàn)動態(tài)編輯表格數(shù)據(jù)

    這篇文章主要為大家詳細(xì)介紹了react+antd實(shí)現(xiàn)動態(tài)編輯表格數(shù)據(jù),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-08-08
  • create-react-app項(xiàng)目配置全解析

    create-react-app項(xiàng)目配置全解析

    這篇文章主要為大家介紹了create-react-app項(xiàng)目配置全解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-06-06
  • 深入學(xué)習(xí)TypeScript 、React、 Redux和Ant-Design的最佳實(shí)踐

    深入學(xué)習(xí)TypeScript 、React、 Redux和Ant-Design的最佳實(shí)踐

    這篇文章主要介紹了深入學(xué)習(xí)TypeScript 、React、 Redux和Ant-Design的最佳實(shí)踐,TypeScript 增加了代碼的可讀性和可維護(hù)性,擁有活躍的社區(qū),,需要的朋友可以參考下
    2019-06-06
  • React Native:react-native-code-push報(bào)錯(cuò)的解決

    React Native:react-native-code-push報(bào)錯(cuò)的解決

    這篇文章主要介紹了React Native:react-native-code-push報(bào)錯(cuò)的解決,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-10-10

最新評論