Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React-JS: Access a child's method through HOC wrapper

I have an Editor component that looks like so:

class EditorComp extends Component {
  focus() {
    this.refs.input.focus();
  }

  render() {
    return (
      <input 
        ref="input"
        ...
      />
    );
  }
}

So that elements that use EditorComp can set a ref and call its focus method and apply focus to the lower level input, like so:

class Parent extends Component {
  render() {
    return (
      <div>
        <button onClick={() => this.refs.editor.focus()}>Focus</button>
        <EditorComp ref="editor" />
      </div>
    );
  }
}

However, when wrapping EditorComp in a Higher Order Component (like react-redux's connect()) the EditorComp loses the focus method because it gets trapped underneath the HOC.

Example:

const WrappedEditor = connect()(EditorComp); // react-redux's connect, for example
const wrappedEditorInstance = <WrappedEditor />;

wrappedEditorInstance.focus() // Error! Focus is not a function.

Is there a way to pass up method or component references through the parent HOCs of a child component?


Edit: Or is there a reverse solution in which the parent hands down a function which sets the focus command? I've considered using an event-emitter, and having the child listen to a focus event called by the parent, however this seems unwieldy and unnecessary.

like image 626
Justin Schultz Avatar asked Jun 27 '16 20:06

Justin Schultz


People also ask

How do I access my child in react?

In React we can access the child's state using Refs. we will assign a Refs for the child component in the parent component. then using Refs we can access the child's state. Creating Refs Refs are created using React.

How do you access child component method in parent component in react native?

One way to call a child component's function from its parent is with the help of the useRef hook. Here's the gist of it: We pass a Ref (e.g. childFunc ) to a child 🧒 component and assign the desired function to the Ref's current key. This then gives you access to that very function in the parent component.

How do you pass a parent method to a child in react?

To pass data from child to parent component in React:Pass a function as a prop to the Child component. Call the function in the Child component and pass the data as arguments. Access the data in the function in the Parent .


1 Answers

1 way return comp instance

class EditorComp extends Component {
  focus() {
    this.refs.input.focus();
  }
  componentDidMount(){
    this.props.onMount(this)
  }
  render() {
    return (
      <input
        ref="input"
        ...
      />
    );
  }
}
export default connect(state=>({state}), actions)(EditorComp);


class Parent extends Component {
  render() {
    return (
      <div>
        <button onClick={() => this.editor.focus()}>Focus</button>
        <EditorComp  onMount={c=>this.editor=c}  ref="editor" />
      </div>
    );
  }
}

2 way pass down position

class EditorComp extends Component {
  componentDidUpdate(prevProps, prevState){
    let {input}= this.refs
    this.props.isFocus? input.focus():input.blur();
  }
  render() {
    return (
      <input
        ref="input"
        ...
      />
    );
  }
}
export default connect(state=>({state}), actions)(EditorComp);


class Parent extends Component {
  render() {
    return (
      <div>
        <button onClick={() => this.setState({isfocus:true});}>Focus</button>
        <EditorComp  isFocus={this.state.isfocus}  ref="editor" />
      </div>
    );
  }
}
like image 129
Kokovin Vladislav Avatar answered Oct 19 '22 23:10

Kokovin Vladislav