zoukankan      html  css  js  c++  java
  • react typescript 父组件调用子组件

    //父组件
    import * as React from 'react'
    import { Input } from 'antd'
    const Search = Input.Search
    import "./index.less"
    import Child from "./compon/list"

    interface IProps {
    MakeMoney?: () => void

    }
    export default class ProjectList extends React.Component<IProps>{
    constructor(props: IProps) {
    super(props)

    }
      
    child: any = {}  //主要加这个
    onRef = (ref) => {
    this.child = ref
    }
    // 调用组件进行通信
    getDS = () => {
    this.child.toggle()

    }
    render(){
    return (
      <div>
        <button onClick={this.getDS}>父组件点击切换</button>
        <Child ref={this.onRef} />
      </div>

    )
    }
    }

    //子组件
    import * as React from 'react'
    import { Row, Col } from 'antd';
    import "./list.less"
    interface IProps {
    msg?: any
    MakeMoney?:any
    //ref?:any

    }
    interface IState {
    lg?: any

    }export default class List extends React.Component<IProps, IState> constructor(props: IProps) {
        super(props)

    }
    state = {
    lg: 6
    }
    toggle = () => {//父组件要调用的方法
    console.log('f')
    this.setState({
    lg: 12
    })
    }

    StudyMakeMoney=()=>{ // 调用父组件方法
    this.props.MakeMoney();
    }
    render(){
    const { lg } = this.state;
    return (
    <div>
      <button onClick={this.StudyMakeMoney}>子组件</button

      </div>
      )
    }
  • 相关阅读:
    Java 多线程之CyclicBarrier
    数据库事务隔离级别
    Java SE之Map接口
    (二)Shiro之一些重要的组件和工具类
    (一)Shiro之简介
    Datatables后端分页
    Spring之BeanFactory中Bean的生命周期
    Servlet之生命周期
    设计模式之单例设计模式
    day041 前端HTML CSS基本选择器(未整理完毕)
  • 原文地址:https://www.cnblogs.com/whlBooK/p/10811792.html
Copyright © 2011-2022 走看看