7

我很难在上下文提供程序中设置 ref。这是不可能的还是我遗漏了什么?我能够以相同的方式在其他组件中创建Refs,但以下始终将 ref 显示为 { current: null }

const VideoPlayerContext = React.createContext();

export default class VideoPlayerProvider extends Component {

  videoPlayerComponent = React.createRef()

  state = {
    // some state
  }

  seek = (time) => {
    this.videoPlayerComponent.seek(time)
  }

  render () {
    return (
      <VideoPlayerContext.Provider value={Object.assign({}, this.state, { seek: seek})}>
        <VideoPlayer 
          key={'videoplayer'} 
          {...this.state}
          ref={this.videoPlayerComponent}
        />
        { this.props.children }
      </VideoPlayerContext.Provider>
    )
  }
}

然后我的应用程序就这样包装了

const app = () => (
  <VideoPlayerProvider>
    <App />
  </VideoPlayerProvider>
)  

任何帮助表示赞赏!

4

0 回答 0