您的当前位置:首页正文

react兄弟组件通信,类组件向函数组件通信

2023-11-07 来源:汇意旅游网
/* eslint-disable react/jsx-indent */
/* eslint-disable import/named */
import React from 'react';
import Top from './top';
import Left from './left';

import Content from './conent';
import './c.less';
class Main extends React.Component {
  constructor(props) {
    super(props);

    this.state = {
      leftcheck: false,
    };
  }
  getChildrenMsg = (result, msg) => {
    this.setState({
      leftcheck: msg,
    });
  };
  render() {
    const { leftcheck } = this.state;
    const topprops = {};

    const leftprops = {
      leftcheck,
      left: 1,
    };

    const contprops = {};
    return (
      <div className="bb">
        <div className="top">
          <Top {...topprops} />
        </div>
        <div className="zz">
          <div className="left">
            <Left parent={this} />
          </div>
          <div className="content">
            <Content xdinfo={leftprops} />
          </div>
        </div>
      </div>
    );
  }
}
export default Main;
/* eslint-disable react/jsx-indent */
/* eslint-disable import/named */
import { CheckBox } from 'choerodon-ui/pro';
import React from 'react';

class Left extends React.Component {
  constructor(props) {
    super(props);
    const { leftcheck, left } = props;
    console.log(left, leftcheck, 'left');
    /**
     * 内部状态
     */
    this.state = {};
  }

  render() {
    const toParent = () => {
      // console.log(this.props.parent.getChildrenMsg.bind(this, this.state.msg))
      this.props.parent.getChildrenMsg(this, this.state.msg);
    };
    const handleChange = val => {
      console.log(val);
      this.props.parent.getChildrenMsg(this, val);

      // toParent(val);
    };
    return (
      <div>
        <CheckBox name="base" onChange={handleChange}>
          A
        </CheckBox>
      </div>
    );
  }
}
export default Left;


import React from 'react';

const content = props => {
  const { xdinfo } = props;

  return <div>{xdinfo.leftcheck ? 1111 : 2222}</div>;
};
export default content;```

因篇幅问题不能全部显示,请点此查看更多更全内容