浅析TypeScript和React中使用ref的方法


当前第2页 返回上一页

1、constructor中onRef绑定this

1

this.props.onRef(this)Copy to clipboardErrorCopied

完成以上4步骤,父组件中可以随便调用子组件中state的值以及方法。

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

export class ParentCom extends React.Component<{}, {}> {

    constructor(props:{}){

        super(props);

        this.onRef = this.onRef.bind(this);

    }

    public child: any;

 

    onRef(ref:any){

        this.child = ref;

    }

 

    getChildFun(){

        this.child.testFun();

    }

 

    render(){

        return (

           <div>

               <span>父组件</span>

               <ChildCom onRef={this.onRef}></ChildCom>

           </div>

        )

    }

}

interface childProps{

    onRef? : any

}

export class ChildCom extends React.Component<childProps, {}> {

    constructor(props:{}){

        super(props);

        this.props.onRef(this);

    }

 

 

    testFun(){

        console.log(123)

    }

 

    render(){

        return (

           <div>

               <span>子组件</span>

           </div>

        )

    }

}

更多编程相关知识,请访问:编程视频!!

以上就是浅析TypeScript和React中使用ref的方法的详细内容,更多文章请关注木庄网络博客

返回前面的内容

相关阅读 >>

javascript怎么下载

java和javascript啥关系

jquery+jsonp跨域需要怎样实现

如何使用javascript改变span标签中的值

浅谈动态导入ecmascript模块的方法

javascript的exec方法怎么用

es6-promise源码的分析

javascript怎么改变文本内容

javascript怎么实现页面的刷新

javascript字符串中指定字符怎么删除

更多相关阅读请进入《typescript》频道 >>




打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...