react中父子组件通信

//父组件
import DividendModal from "./dividendModal"
class Dividend extends Component {
	constructor(props) {
    	super(props);
    	//this.dividendModal= React.createRef();   ⑶
  	}
    dividendRecord = (id, name) => {
        //第一种ref调用子组件的方法   
        this.dividendModal.handleVisible()  // ⑴
        //第二种ref的写法
        //this.refs.dividendModal  ⑵
        //第三种ref对节点的引用可以通过ref的current属性得到
        //this.dividendModal.current   ⑶
        this.dividendModal.props.form.setFieldsValue({
            fund: ""
        })
    }
    getInfo = (value) => {
        this.setState({
            value
        })
    }
    render() {
        return (
            <>
                <DividendModal
                    {...this.props}
                    {...this.state}
                    smallColumns={smallColumns}
                    rowKey={'nId'}
                    //回调函数   ⑴
                    ref={node => this.dividendModal = node}  
                    //字符串形式  ⑵
                    //ref="dividendModal"  
                    //React.createRef() ⑶   
                    //ref= {this.dividendModal}   
                    handleInfo={this.getInfo}
                />
            </>
        )
    }
}
//子组件
class DividednModal extends Component {
    //通过this.props调用父组件的属性和方法
	handleVisible = () => {
        this.setState({
            visible: true
        })
        //通过props的方法把值传给父组件
        this.props.handleInfo("react子组件传值啦!")
    }
 }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值