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

114
Views
How to return custom component from another function in react JS?

This is my first.js file

import Second from "./second";

function ClickHandler() {
    return (
      <div>
        <Second></Second>
      </div>
    );
 }


function First() {

  return (
    <div>
      <div>This is first</div>

      <div>
        <button onClick={ClickHandler}>Click it</button>
      </div>
    </div>
  );
}
export default First;

This is my second.js file

function Second(){
    return(
        <div>
            <div>
                This is Second
            </div>
        </div>
    );
}
export default Second;

I want to disply the content of second.js file when button gets clicked. But this is not working as expected? Any solution for this? Please...

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

0

import React, { useState } from 'react'
import Second from "./second";



function First() {
 const [secondComponentVisibility, setSecondComponentVisibility] = 
 useState(false);

  const ClickHandler = () => {
    setSecondComponentVisibility(true)
  }
  
  return (
    <div>
      <div>This is first</div>

      <div>
        <button onClick={ClickHandler}>Click it</button>
      </div>

      {secondComponentVisibility && <Second/>}
    </div>
  );
}
export default First;


about 4 years ago · Juan Pablo Isaza Report

0

function First() {
   const [showSecond, setShowsecond] = useState(false);
  return (
   showSecond ? <SecondComponent/> : <div>
      <div>This is first</div>
      <div>
        <button onClick={()=>setShowsecond(true)}>Click it</button>
      </div>
    </div>
  );
}
export default First;

hope this will help

about 4 years ago · Juan Pablo Isaza Report

0

first.js

import React, { useState } from react

import Second from './second'

function First() {
    const [IsSecondShowing, SetIsSecondShowing] = useState(false)

    return (
        <div>
            <div>This is first</div>
            {IsSecondShowing ? <Second /> : null}
            <div>
                <button onClick={() => SetIsSecondShowing(prev => !prev)}>
                    Click it
                </button>
            </div>
        </div>
    )
}
export default First

second.js

import React from 'react'

function Second() {
    return (
        <div>
            <div>This is Second</div>
        </div>
    )
}
export default Second
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!