美文网首页
React学习总结6--children

React学习总结6--children

作者: 琉璃_xin | 来源:发表于2019-08-15 20:12 被阅读0次

demos源码
每个组件都可以获取到 props.children。它包含组件的开始标签和结束标签之间的内容。

react提供了一系列的方法来方便处理children。

  • map
  • forEach
  • count,
  • toArray
  • only

实际应用

在实际项目中的应用,我们可以对默认的children进行增加处理来达到我们的目的。很多的UI组件就使用了这个属性。
antd的Button组件:

<Button type="primary">Primary</Button>

最终实现效果:

<button type="button" class="ant-btn ant-btn-primary"><span>Primary</span></button>

模拟实现:

import React, { Component } from "react";
import PropTypes from "prop-types";
import classNames from "classnames";

import("./index.css");

const rxTwoCNChar = /^[\u4e00-\u9fa5]{2}$/;
const isTwoCNChar = rxTwoCNChar.test.bind(rxTwoCNChar);
function isString(str) {
    return typeof str === "string";
}

function insertSpace(child, needInserted) {
    if (child == null) {
        return;
    }
    const SPACE = needInserted ? " " : "";
    if (
        typeof child !== "string" &&
        typeof child !== "number" &&
        isString(child.type) &&
        isTwoCNChar(child.props.children)
    ) {
        return React.cloneElement(
            child,
            {},
            child.props.children.split("").join(SPACE)
        );
    }

    if (typeof child === "string") {
        if (isTwoCNChar(child)) {
            child = child.split("").join(SPACE);
        }
        return <span>{child}</span>;
    }
    return child;
}

class Button extends Component {
    static defaultProps = {
        htmlType: "button",
        type: "default"
    };

    static propTypes = {
        type: PropTypes.string,
        className: PropTypes.string
    };

    handleClick = e => {
        const { onClick } = this.props;
        if (onClick) {
            onClick(e);
        }
    };

    render() {
        const { type, className, children, htmlType, ...rest } = this.props;
        const kids = insertSpace(children, true);
        const classes = classNames("ant", className, {
            [`ant-${type}`]: type
        });

        const buttonNode = (
            <button
                type={htmlType}
                className={classes}
                onClick={this.handleClick}
                {...rest}
            >
                {kids}
            </button>
        );

        return buttonNode;
    }
}

export default Button;
import React, { Component } from 'react';
import Button from './Button';

class ChildrenCom extends Component {
  render() {
    return (
      <div>
        <Button type="primary">添加</Button>
        <Button onClick={(e) => {alert(e)}}>删除</Button>
      </div>
    )
  }
}

export default ChildrenCom;

上边实现过程中使用了React.cloneElement,使用方法如下:

React.cloneElement(
  element,
  [props],
  [...children]
)

const {children,...otherPorps}=this.porps
React.Children.map(children,child=>{
    React.cloneElement(child,otherPorps)
})

相关文章

  • React学习总结6--children

    demos源码每个组件都可以获取到 props.children。它包含组件的开始标签和结束标签之间的内容。 re...

  • 2019年10月-11月学习内容总结

    学习内容总结 前端 React主要的学习路径是在React的官网以及阮一峰大佬的教程; React Hook Ne...

  • react-hooks

    前置 学习面试视频 总结react hooks react-hooks react-hooks为函数组件提供了一些...

  • react学习(2019/7/30-8/7)

    今日总结 课上提到的内容 1. 讲了react要学习的内容提纲 react 基础,JSX语法,插件 react-r...

  • React 基础学习总结

    React 基础学习总结 1、创建虚拟DOM对象的两种方式 React.createElement(type, p...

  • React 学习总结

    官网地址https://facebook.github.io/react/docs/hello-world.htm...

  • react学习总结

    1.安装npm install -g create-react-appcreate-react-app 文件名cd...

  • React学习总结

    第1章 课程导学 学习前提要有一些js、es6、webpack、npm等基础知识 第2章 React初探 2-1 ...

  • React学习总结

    创建react应用 npm install create-react-app -g html写在了哪个js文件中,...

  • React Native如何集成到现有项目中

    本系列文章作为学习RN期间的总结 React Native如何集成到现有项目中 React Native和Nati...

网友评论

      本文标题:React学习总结6--children

      本文链接:https://www.haomeiwen.com/subject/zzttsctx.html