Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

136
Views
React not rendering a new component on button click based on props

I was trying to make a function that takes in props and then render a component on button click but for some reason the code wont render any component on click nor there are any errors in the console log here is the code

const App = () => {
    const RenderVideoPlayer = (props) => {
        let videoSrc = {
            type: 'video',
            sources: [
                {
                  src: `${props.video_url}`,
                  type: 'video/mp4',
                  size: 1080
                }
            ],
            poster: '/path/to/poster.jpg',
        }
        return (
            <Plyr
                options={options}
                source={videoSrc}
            />
        )
    }


return (
<Button className="s-btn-1" variant="primary" onClick={() => RenderVideoPlayer(movie.video_info[0])}>Watch</Button>
)

I can confirm that it is receiving the props onclick

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Because that isn't at all how React works. Returning a component to the onClick event isn't going to render that component. Instead, put the component in your returned JSX, but conditionally render it based on state. Then in the click handler simply update that state. For example:

const [showPlayer, setShowPlayer] = useState(false);

return (
  <>
    <Button onClick={() => setShowPlayer(true)}>Watch</Button>
    { showPlayer ? <Plyr ... /> : null }
  </>
);

This is obviously very simplistic, but demonstrates the concept. You can make the button into a show/hide toggle by using !showPlayer instead of true for example. Or you could show/hide multiple players by tracking which ones are shown/hidden in state and then rendering an array of them based on that state. And so on.

But the main point is that you can't just return JSX from an event handler and expect React to do anything with that. Rely on state for tracking all of the information you need to render the result. The JSX uses state in the rendering, and events update state.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!