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>
  );
}
3
  • 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. Commented Mar 13, 2019 at 12:31
  • 1
    Have you checked this: stackoverflow.com/questions/33956201/… Commented Mar 13, 2019 at 12:32
  • 1
    Then you might want to lift state up and move the rendering of the buttons from KokPlayer to the footer instead. Commented Mar 13, 2019 at 12:45

1 Answer 1

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>
  );
}
Sign up to request clarification or add additional context in comments.

2 Comments

But this solution does not pass on the functions of the buttons which are defined in KokPlayer.js, this.play and so on
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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.