使用React从另一个组件添加到组件

时间:2019-03-13 12:27:45

标签: javascript reactjs

我有一个在footer中创建的App.js,现在有另一个名为KokPlayer.js的组件,我想将按钮从KokPlayer.js添加到{{1} }在footer中。

我该怎么做?

App.js

App.js

KokPlayer.js

render() {
  const { expanded } = this.state;

  return (
    <React.Fragment>
      <nav
        className="footer navbar navbar-light bg-success mb-auto"
        ref="footerRef"
      />
    </React.Fragment>
  );
}

1 个答案:

答案 0 :(得分:1)

据我所知,您可以在KokPlayer组件中传递一个道具,然后隐藏不想在App.js中显示的元素

App.js

import KokPlayer from './KokPlayer' // KokPlayer location

....

render() {
  const { expanded } = this.state;

  return (
    <React.Fragment>
      <nav
        className="footer navbar navbar-light bg-success mb-auto"
        ref="footerRef"
      />
     <KokPlayer showButtonsOnly={true} /> // the buttons here
    </React.Fragment>
  );
}

KokPlayer.js

render() {
  // Add this block to footer
  return (
   <div>
     {!this.props.showButtonsOnly && <div> // it wouldn't be shown in App.js
        Screen Here 
     </div>}
    <div style={{ display: "block", margin: "0 auto", verticalAlign: "middle" }}
    >
      <Button onClick={this.play} className="mr-3">
        play()
      </Button>
      <Button onClick={this.pause} className="mr-3">
        pause()
      </Button>
      <Button onClick={this.toggleFullScreen} className="mr-3">
        FullScreen()
      </Button>
    </div>
   </div>
  );
}