React插槽 之React.Children的使用小结

 更新时间:2026年06月20日 10:26:36   作者:英俊潇洒美少年  
本文深入解析React.Children API的使用方法,包括map、forEach、count、only等实用功能,通过具体代码示例展示如何管理和操作props.children,感兴趣可以了解一下

React.Children 提供了用于处理 this.props.children 不透明数据结构的实用方法。

React.Children.map

React.Children.map(children, function[(thisArg)])

在 children 里的每个直接子节点上调用一个函数,并将 this 设置为 thisArg。如果 children 是一个数组,它将被遍历并为数组中的每个子节点调用该函数。如果子节点为 null 或是 undefined,则此方法将返回 null 或是 undefined,而不会返回数组。

如果 children 是一个 Fragment 对象,它将被视为单一子节点的情况处理,而不会被遍历。

React.Children.forEach

React.Children.forEach(children, function[(thisArg)])

与 React.Children.map() 类似,但它不会返回一个数组。

React.Children.count

React.Children.count(children)

返回 children 中的组件总数量,等同于通过 map 或 forEach 调用回调函数的次数。

React.Children.only

React.Children.only(children)

验证 children 是否只有一个子节点(一个 React 元素),如果有则返回它,否则此方法会抛出错误。

注意:

React.Children.only() 不接受 React.Children.map() 的返回值,因为它是一个数组而并不是 React 元素。

React.Children.map 的用例

import React, {Component} from 'react';
import {connect} from 'react-redux';
import {Steps} from 'antd';
import {setStepWillChange, setConfirmStep} from 'actions/common';
import './index.less';
import Final from 'src/project';
import Temp from '../temp/index';
import Storage from 'fw/util/storage';
import util from 'fw/util/util';
class StepView extends Component {

    componentWillMount() {
        setConfirmStep(this.props.dispatch, this.props.stepSource.length - 3);
    }

    render() {
        let visitFrom = Storage.getSession('visitFrom') || util.getUrlParam('visitFrom');
        console.log('%c 👈 张浩雨: StepView -> render -> React.Children.map ', 'font-size:16px;background-color:#44ce68;color:white;', React.Children.map)
        console.log('%c 🔩 张浩雨: StepView -> render -> this.props.children ', 'font-size:16px;background-color:#51e51d;color:black;', this.props.children)
        return (
            <div className="steps-wrap" style={{width: '1200px', margin: '30px auto', backgroundColor: '#FFFFFF'}}>
                <Steps current={this.props.stepIndex} maxDescriptionWidth={this.props.maxDescriptionWidth || 100}>
                    {
                        this.props.stepSource.map((v, i) => {
                            return <Steps.Step key={i} title={v.title}/>;
                        })
                    }
                </Steps>
                {
                    React.Children.map(this.props.children, (child, i) => {
                        return <div style={{display: (i === this.props.stepIndex ? 'block' : 'none')}}>
                            {child}
                        </div>;
                    })
                }
                <div className="step-btn-content" style={{padding:'20px 0'}}>
                    <a type="primary" className="step-btn preview-btn"
                       style={{display: ((this.props.stepIndex > this.props.confirmIndex) || this.props.stepIndex === 0) ? 'none' : 'inline-block'}}
                       onClick={() => {
                           setStepWillChange(this.props.dispatch, this.props.stepIndex - 1);
                       }}>上一步</a>
                    {
                        Final.needTemp && (this.props.needTemp !== false) && this.props.stepIndex <= this.props.confirmIndex &&
                        <Temp temp={this.props.temp} reFull={this.props.reFull} getTemp={this.props.getTemp}/>
                    }
                    {this.props.stepIndex !== this.props.confirmIndex && <a type="primary" className="step-btn next-btn"
                                                                            style={{display: this.props.stepIndex === this.props.confirmIndex + 1 ? 'none' : 'inline-block'}}
                                                                            onClick={() => {
                                                                                setStepWillChange(this.props.dispatch, this.props.stepIndex + 1);
                                                                            }}>下一步</a>}
                    <a type="primary" id="submitStepBtnText" className={visitFrom=='shzww'?'step-btn next-btn shzww-tj':'step-btn next-btn'}
                       style={{display: this.props.stepIndex === this.props.confirmIndex ? 'inline-block' : 'none'}}
                       onClick={() => {
                           setStepWillChange(this.props.dispatch, this.props.stepIndex + 1);
                       }}>提交</a>
                </div>
            </div>
        );
    }
}

export default connect(
    (state) => {
        return {
            stepIndex: state.common.stepIndex,
            confirmIndex: state.common.confirmIndex
        };
    }
)(StepView);

{
     React.Children.map(this.props.children, (child, i) => {
         return <div style={{display: (i === this.props.stepIndex ? 'block' : 'none')}}>
             {child}
         </div>;
     })
 }
//! zhy added 特别纳税调整相互协商程序申请
import React, {Component} from 'react';
import StepView from 'biz/components/steps/index';
import Wc,{ylztRender} from 'src/common/wc';
import {setConfirmStep} from 'actions/common';
import {connect} from 'react-redux';
import Sqtx from './Sqtx';
import YlTj from './Yltj';
import { jblRender } from 'biz/components/blzt/jbl';
import {option} from './option';

const stepSource = [
   {
       title: '填写申请表'
   },
   {
       title: '预览提交'
   },
   {
       title: '审核中'
   },
   {
       title: '完成'
   }
];
const constBlzt = {
   swsxMc: option.swsxMc,
   blzt: jblRender
}

class Index extends Component {
   componentDidMount() {
       setConfirmStep(this.props.dispatch, 1);
   }
   
   temp(callback){ //todo 暂存
       let data = this.Sqtx.getWrappedInstance().getTemp();
       let requestData = {
           swsxDm: this.props.swsxDm,
           data: JSON.stringify(data),
           sqxh: this.props.sqxh
       };
       callback(requestData);
   }
   
   getTemp(data){ //todo 获取暂存
       let tempData = JSON.parse(data.data);
       this.Sqtx.getWrappedInstance().setTemp(tempData);
   }
   render() {
       const {stepIndex, stepWillChangeTo, blztDm} = this.props;
       let temp = true;
       return (
           <div>
               {
                   ylztRender(<StepView stepSource={stepSource} 
                               needTemp={temp} 
                               temp={(callback) => { this.temp(callback)}} 
                               getTemp={(data) => {this.getTemp(data)}}
                              >
                       <Sqtx ref={ref => { this.Sqtx = ref}} 
                           stepIndex={stepIndex} 
                           stepWillChangeTo={stepWillChangeTo} 
                           indexKey={0}
                       />
                       <YlTj ylType="pdf" 
                           stepIndex={stepIndex} 
                           stepWillChangeTo={stepWillChangeTo} 
                           indexKey={1}
                       />
                       <Wc ylComponent={YlTj}
                           constBlzt={constBlzt} 
                           stepIndex={stepIndex} 
                           stepWillChangeTo={stepWillChangeTo} 
                           indexKey={2} 
                           swsx={option.swsxDm} 
                           blzt={blztDm}
                       />
                   </StepView>, YlTj, constBlzt)
               }
           </div>
       );
   }
}

export default connect(state => {
   return {
       stepIndex: state.common.stepIndex,
       stepWillChangeTo: state.common.stepWillChangeTo,
       blztDm: state.common.blztStore.blztDm,
       swsxDm: state.common.swsxDm,
       sqxh: state.common.sqxh,
       area:state.common.area
   };
})(Index);

到此这篇关于React插槽 之React.Children的使用小结的文章就介绍到这了,更多相关React.Children 使用内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • React插槽使用方法

    React插槽使用方法

    本文主要介绍了React插槽使用方法,文中通过示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2021-12-12
  • react-native 封装选择弹出框示例(试用ios&android)

    react-native 封装选择弹出框示例(试用ios&android)

    本篇文章主要介绍了react-native 封装选择弹出框示例(试用ios&android),具有一定的参考价值,有兴趣的可以了解一下
    2017-07-07
  • react项目使用json-server模拟接口获取数据的操作方法

    react项目使用json-server模拟接口获取数据的操作方法

    文章介绍了如何在React项目中使用json-server模拟接口并获取数据,首先安装json-server,创建JSON格式的数据文件,并在package.json中添加启动命令,启动服务后,可以通过指定的路径获取模拟的数据,感兴趣的朋友一起看看吧
    2025-11-11
  • React Hooks useReducer 逃避deps组件渲染次数增加陷阱

    React Hooks useReducer 逃避deps组件渲染次数增加陷阱

    这篇文章主要介绍了React Hooks 之 useReducer 逃避deps后增加组件渲染次数的陷阱详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-09-09
  • 使用react实现手机号的数据同步显示功能的示例代码

    使用react实现手机号的数据同步显示功能的示例代码

    本篇文章主要介绍了使用react实现手机号的数据同步显示功能的示例代码,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-04-04
  • react hooks中的useState使用要点

    react hooks中的useState使用要点

    这篇文章主要为大家介绍了react hooks中的useState使用要点详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-09-09
  • React diff算法原理详细分析

    React diff算法原理详细分析

    经典的diff算法中,将一棵树转为另一棵树的最低时间复杂度为 O(n^3),其中n为树种节点的个数。假如采用这种diff算法,一个应用有1000个节点的情况下,需要比较十亿次才能将dom树更新完成,显然这个性能是无法让人接受的
    2022-11-11
  • 深入理解React与闭包的关系

    深入理解React与闭包的关系

    本文将深入探讨React与闭包之间的关系,我们将首先介绍React和闭包的基本概念,然后详细解释React组件中如何使用闭包来处理状态和作用域的问题,希望通过本文的阅读,你将对React中闭包的概念有更深入的理解,并能够在开发React应用时更好地应用闭包
    2023-07-07
  • React Native悬浮按钮组件的示例代码

    React Native悬浮按钮组件的示例代码

    本篇文章主要介绍了React Native悬浮按钮组件的示例代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面来一起看看吧。
    2018-04-04
  • 详解在React项目中如何集成和使用web worker

    详解在React项目中如何集成和使用web worker

    在复杂的React应用中,某些计算密集型或耗时操作可能会阻塞主线程,导致用户界面出现卡顿或响应慢的现象,为了优化用户体验,可以采用Web Worker来在后台线程中执行这些操作,本文将详细介绍在React项目中如何集成和使用Web Worker来改善应用性能,需要的朋友可以参考下
    2023-12-12

最新评论