/**
* Created by mapbar_front on 2019/8/5.
*/
import React,{Component} from 'react';
function handleClick(ref){
console.log('handleClick',ref.current); //<input type="text"/>
ref.current.focus()
}
const Test = React.forwardRef((props,ref)=>{
console.log('ref',ref.current); //null
return <div>
<input type="text" ref={ref} />
<button onClick={()=>handleClick(ref)}>
{props.children}
</button>
</div>
})
class Ref3 extends Component{
constructor(props){
super(props);
this.ref = React.createRef();
}
handleClick(){
console.log('this.ref');
}
render(){
return(
<Test ref={this.ref}>
点我
</Test>
)
}
}
export default Ref3;
/**
* Created by mapbar_front on 2019/8/6.
*/
import React,{Component} from 'react';
const FancyButton=React.forwardRef((props,ref)=>{
return <div>
<input ref={ref} type="text"/>
<button onClick={props.theme}>
{props.children}
</button>
</div>
})
class Ref12 extends Component{
constructor(props){
super(props);
this.ref = React.createRef();
}
handleClick=()=>{
console.log('this.ref',this.ref);
this.ref.current.focus();
}
render(){
return(
<div>
<FancyButton ref={this.ref} theme={ this.handleClick }>
FancyButton
</FancyButton>
</div>
)
}
}
export default Ref12;