Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

224
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda