You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
//ClassclassInputWithFocusextendsReact.Component{this.inputRef=null;render(){return(<><inputref={inputRef=>{this.inputRef=inputRef;}}/><buttononClick={()=>this.inputRef.focus()}>Focus the input</button></>);}}//HooksconstInputWithFocus=props=>{constinputRef=useRef();return(<><inputref={inputRef}/><buttononClick={()=>inputRef.current.focus()}>Focus the input</button></>);};
//Class (method 1)constructor(props){super(props)this.myCallBack=this.myCallBack.bind(this)}//Class (method 2, better)myCallBack=(props)=>{this.props.doSomething()}//hook way// if you want callback to be refreshed by props.doSomething changes:functionMyComponent(props){useCallback(()=>{props.doSomething()},[props.doSomething()])}// if you want callback to be take into account state value// (otherwise it won't know about upper scope state values, it will just know state value at mount - not current one -):functionMyComponent(props){const[someValue,setSomeValue]=useState('');useCallback(()=>{props.doSomething(someValue)},[someValue])}
Hi, thanks @MacKentoch !!!!!!