溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

react children方法如何使用

發(fā)布時間:2023-01-03 10:02:52 來源:億速云 閱讀:88 作者:iii 欄目:web開發(fā)

這篇文章主要介紹“react children方法如何使用”的相關知識,小編通過實際案例向大家展示操作過程,操作方法簡單快捷,實用性強,希望這篇“react children方法如何使用”文章能幫助大家解決問題。

react children方法用于處理“this.props.children”,其處理方式有:1、React.Children.map();2、React.Children.forEach();3、React.Children.count();4、React.Children.only();5、React.Children.toArray()。

React.Children詳解

React.Children提供了處理this.props.children的工具,this.props.children可以任何數(shù)據(jù)(組件、字符串、函數(shù)等等)。React.children有5個方法:React.Children.map(),React.Children.forEach()、React.Children.count()、React.Children.only()、React.Children.toArray(),通常與React.cloneElement()結(jié)合使用來操作this.props.children。

  • React.Children.map()

React.Children.map()有些類似Array.prototype.map()。如果children是數(shù)組則此方法返回一個數(shù)組,如果是null或undefined則返回null或undefined。第一參數(shù)是children,即示例中的Father組件里的'hello world!'和() => <p>2333</p>函數(shù)。第二個參數(shù)是fucntion,function的參數(shù)第一個是遍歷的每一項,第二個是對應的索引。

function Father({children}) {
    return(
      <div>
      {React.Children.map(children, (child, index) => {
          ...
      })}
      </div>    
    )        
 }
<Father>
    hello world!
    {() => <p>2333</p>}
</Father>

  • React.Children.forEach()

  跟React.Children.map()一樣,區(qū)別在于無返回。

  • React.Children.count()

React.Children.count()用來計數(shù),返回child個數(shù)。不要用children.length來計數(shù),如果Father組件里只有'hello world!'會返回12,顯然是錯誤的結(jié)果。

function Father({children}) {
    return(
      <div>
      {React.Children.count(children)}
      </div>    
    )        
 }
<Father>
    hello world!
    {() => <p>2333</p>}
</Father>

  • React.Children.only()

  驗證children里只有唯一的孩子并返回他。否則這個方法拋出一個錯誤。

function Father({children}) {
    return(
      <div>
      {React.Children.only(children)}
      </div>    
    )        
 }
<Father>
    hello world!
</Father>

  • React.Children.toArray()

  將children轉(zhuǎn)換成Array,對children排序時需要使用

function Father({children}) {
    let children1 = React.Children.toArray(children);
    return(
      <div>
      {children1.sort().join(' ')}
      </div>    
    )        
 }
<Father>
    {'ccc'}
    {'aaa'}
    {'bbb'}
</Father>
//渲染結(jié)果: aaa bbb ccc

如果不用React.Children.toArray()方法,直接寫children.sort()就會報錯

react children方法如何使用

Example:

例如有這樣的需求,完成一個操作需要3個步驟,每完成一個步驟,對應的指示燈就會點亮。

index.jsx

import * as React from 'react';
import * as ReactDOM from 'react-dom';
import {Steps, Step} from './Steps';
function App() {
    return (
        <div>
        <Steps currentStep={1}>  //完成相應的步驟,改變currentStep的值。如,完成第一步currentStep賦值為1,完成第二部賦值為2
            <Step />
            <Step />
            <Step />
            </Steps>
        </div>
    );
}
ReactDOM.render(<App />, document.getElementById('root'));

Steps.jsx

import * as React from 'react';
import './step.less';
function Steps({currentStep, children}) {
    return (
        <div>
         {React.Children.map(children, (child, index) => {
            return React.cloneElement(child, {
              index: index,
              currentStep: currentStep
            });
         })}
      </div>
    );
}
function Step({index, currentStep}: any) {
    return <div className={`indicator${currentStep >= index + 1 ? ' active' : ''}`} />;
}
export {Steps, Step};

steps.less

.indicator { display: inline-block; width: 100px; height: 20px; margin-right: 10px; margin-top: 200px; background: #f3f3f3; &.active {
    background: orange;
  }

關于“react children方法如何使用”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關的知識,可以關注億速云行業(yè)資訊頻道,小編每天都會為大家更新不同的知識點。

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI