0

I have a footer that I created in App.js and now I have another component called KokPlayer.js and I want to add buttons from KokPlayer.js to the footer in App.js.

How can I do that?

App.js

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

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

KokPlayer.js

render() {
  // Add this block to footer
  return (
    <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>
  );
}
Tholle
  • 108,070
  • 19
  • 198
  • 189
Richard
  • 1,087
  • 18
  • 52
  • 2
    The easiest way is probably to import the component in `KokPlayer.js` to `App.js` and render the component as a child to the footer. – Tholle Mar 13 '19 at 12:31
  • 1
    Have you checked this: https://stackoverflow.com/questions/33956201/how-to-import-and-export-components-using-react-es6-webpack – Artur Carvalho Mar 13 '19 at 12:32
  • 1
    Then you might want to [lift state up](https://reactjs.org/docs/lifting-state-up.html) and move the rendering of the buttons from `KokPlayer` to the footer instead. – Tholle Mar 13 '19 at 12:45

1 Answers1

1

As I understand what you wrote in the comments, you can pass a prop to KokPlayer component then you can hide the elements that you don't want to show them up in App.js

in 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>
  );
}
Liam
  • 6,517
  • 7
  • 25
  • 47
  • But this solution does not pass on the functions of the buttons which are defined in `KokPlayer.js`, this.play and so on – Richard Mar 13 '19 at 13:13
  • Yes, it's doesn`t pass the functions of the buttons but the buttons functions are working. if you want to pass the functions to App.js component you can use Lift state up – Liam Mar 13 '19 at 13:51