最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin4浏览0评论

I have an Editor ponent 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 ponent references through the parent HOCs of a child ponent?


Edit: Or is there a reverse solution in which the parent hands down a function which sets the focus mand? 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.

I have an Editor ponent 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 ponent references through the parent HOCs of a child ponent?


Edit: Or is there a reverse solution in which the parent hands down a function which sets the focus mand? 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.

Share Improve this question edited Aug 31, 2016 at 12:07 Kokovin Vladislav 10.4k5 gold badges40 silver badges36 bronze badges asked Jun 27, 2016 at 20:39 Justin SchultzJustin Schultz 4404 silver badges12 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 8

1 way return p instance

class EditorComp extends Component {
  focus() {
    this.refs.input.focus();
  }
  ponentDidMount(){
    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 {
  ponentDidUpdate(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>
    );
  }
}
发布评论

评论列表(0)

  1. 暂无评论