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

222
Views
Conditional rendering a component with using Radio button

New Tasks.js

import React, { useState } from "react";
import './NewTask.css';
import TaskDetails from "./TaskDetails";
import TaskSetting from "./TaskSetting";

const NewTask = () => {
    const [checked, setChecked] = useState("inPerson");


    return (
        <div className="newtask-div">
            <h3 className="header">New Task</h3>
            <ul className="task-item">
                <li style={{ fontSize: "17px" }}>Select Task Type: </li>
                <li>
                    <input
                        type="radio"
                        checked={checked === "inPerson"}
                        name="inPerson" value="inPerson"
                        onChange={(e) => {
                            setChecked(e.target.value)
                            console.log("show task details")
                        }}
                    />
                    <lable>In Person</lable>
                </li>
                <li>
                    <input
                        type="radio"
                        checked={checked === "online"}
                        name="online" value="online"
                        onChange={(e) => {
                            setChecked(e.target.value)
                            console.log("dont show task detail")
                        }}
                    />
                    <lable>Online</lable>
                </li>
            </ul>

            <TaskDetails />

        </div>
    )
}
export default NewTask

I am trying to use the radio button to conditional rendering the Component. Ex: when clicking the "inPerson" it will render out the "TaskDetail" component and show others component when using "online". Is there any method to achieve this?

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

0

You can render components conditionally based on selected value like below:

...

{
   checked === "inPerson" && 
   <InpersonComponents />
}
{
   checked === "online" && 
   <OnlineComponents />
}

...
about 4 years ago · Juan Pablo Isaza Report

0

I recommend this method with conditional rendering

{checked === "inPerson" ? <TaskDetails /> : <SomeOtherComponent />

or

{checked === "inPerson" && <TaskDetails />}
{checked === "online" && <SomeOtherComponent />}
about 4 years ago · Juan Pablo Isaza Report

0

name of inputs should be same if you want to select anyone of them

    const NewTask = () => {
        const [inPerson, setInPerson] = useState(true);
        const [online, setOnline] = useState(false);
    
    
        return (
            <div className="newtask-div">
                <h3 className="header">New Task</h3>
                <ul className="task-item">
                    <li style={{ fontSize: "17px" }}>Select Task Type: </li>
                    <li>
                        <input
                            type="radio"
                            checked={online}
                            name="singlegroup" value="inPerson"
                            onChange={(e) => {
                                setInPerson(e.target.value)
                                setOnline(!e.target.value)
                                console.log("show task details")
                            }}
                        />
                        <lable>In Person</lable>
                    </li>
                    <li>
                        <input
                            type="radio"
                            checked={inPerson}
                            name="singlegroup" value="online"
                            onChange={(e) => {
                                setOnline(e.target.value)
                                setInPerson(!e.target.value)
                                console.log("dont show task detail")
                            }}
                        />
                        <lable>Online</lable>
                    </li>
                </ul>
    
                <TaskDetails />
                {
                online && <Component1/>                
}
{
                inPerson && <Component2/>                
}
    
            </div>
        )
    }
    export default NewTask
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!