1基本使用

这里的组合继承类似与vue中的插槽,子组件的内容由父组件来决定。
子组件

  1. import React, { FC } from "react"
  2. const Splitpane:FC=(props)=>{
  3. return <div style={{backgroundColor:"red"}}>
  4. {props.children}
  5. </div>
  6. }
  7. export default Splitpane

父组件

  1. import React from 'react';
  2. import ReactDOM from 'react-dom';
  3. import './index.css';
  4. import * as serviceWorker from './serviceWorker';
  5. import Splitpane from "@/components/splitpane"
  6. ReactDOM.render(
  7. <Splitpane><div>这是split的子元素</div></Splitpane>,
  8. document.getElementById('root')
  9. );
  10. serviceWorker.unregister();

2.定义左右两边内容

在父组件中定义left与right两个变量,变量分别对应左右两块内容,在子组件的props中进行接收

  1. import React from 'react';
  2. import ReactDOM from 'react-dom';
  3. import './index.css';
  4. import * as serviceWorker from './serviceWorker';
  5. import Splitpane from "@/components/splitpane"
  6. ReactDOM.render(
  7. <Splitpane left={<div>左边内容</div>} right={<div>有那边内容</div>}><div >这是split的子元素</div></Splitpane>,
  8. document.getElementById('root')
  9. );
  10. serviceWorker.unregister();
  1. import React, { FC } from "react";
  2. //定义左右两块的类型
  3. type IProps = {
  4. left: JSX.Element;
  5. right: JSX.Element;
  6. };
  7. const Splitpane: FC<IProps> = (props) => {
  8. return <div style={{ backgroundColor: "red" }}>
  9. <div style={{float:"left"}}>{props.left}</div>
  10. <div style={{float:"right"}}>{props.right}</div>
  11. {props.children}</div>;
  12. };
  13. export default Splitpane;