TypeScript & RN & refs使用

方式一:

export default class InputView extends React.Component{
    refs:{
        [key: string]: (any);
        input:RN.TextInput;
    }
    constructor(props:any)
    {
        super(props);
    }

    private _onClick():void{
        let v = this.refs["input"];
        v.clear();
        ToastAndroid.show(v +"  ",ToastAndroid.LONG);
    }

    render()
    {
        return(
            
                
                
                    ADD
                
            
        );
    }
}

方式二:

interface IRefKey{
    input?:React.TextInput;
}
export default class InputView extends React.Component{
    refKey : IRefKey = {};
    constructor(props:any)
    {
        super(props);
    }

    private _onClick():void{
        this.refKey.input.clear();
    }

    render()
    {
        return(
            
                 this.refKey.input = ref} placeholder="请输入内容" style={{width:200}}/>
                
                    ADD
                
            
        );
    }
}

参考:
How to use refs in react with Typescript

stackoverflow

TextInput

你可能感兴趣的:(TypeScript & RN & refs使用)