美文网首页
父子组件

父子组件

作者: 一土二月鸟 | 来源:发表于2020-08-16 20:49 被阅读0次

创建父子组件的两种方式

  • 方式一
import React from 'react';
import ReactDOM from 'react-dom';

const Parent = (props) => {
  return <>
    i am parent
    <ChildSon />
    <ChildDaughter />
  </>;
}

const ChildSon = () => {
  return <p>i am son</p>;
}

const ChildDaughter = () => {
  return <p>i am daughter</p>;
}

const Index = () => <>
  <Parent></Parent>
</>

ReactDOM.render(<Index />, document.getElementById('root'));
  • 方式二
import React from 'react';
import ReactDOM from 'react-dom';

const Parent = (props) => {
  return <>
    i am parent
    {props.children}
  </>;
}

const ChildSon = () => {
  return <p>i am son</p>;
}

const ChildDaughter = () => {
  return <p>i am daughter</p>;
}

const Index = () => <>
  <Parent>
    <ChildSon />
    <ChildDaughter />
  </Parent>
</>

ReactDOM.render(<Index />, document.getElementById('root'));

相关文章

网友评论

      本文标题:父子组件

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