Answers for "pass ref to class component react"

0

pass ref to class component react

class ElemComponent extends Component {
  render() {
    return (
      <div ref={this.props.innerRef}>
        Div has a ref
      </div>
    )
  }
}

export default React.forwardRef((props, ref) => <ElemComponent 
  innerRef={ref} {...props}
/>);
Posted by: Guest on August-30-2021
0

passing ref to child component

// Dumb:
export default props =>
    <input type='number' ref={props.setRef} />

// Smart:
class Parent extends Component {
    constructor(props) {
        super(props);
    }

    setRef(ref) {
        this.inputRef = ref;
    }

    render(){
        return <Dumb {...this.props} setRef={this.setRef} />
    }
}
Posted by: Guest on July-16-2021

Code answers related to "pass ref to class component react"

Browse Popular Code Answers by Language