0

I am trying to create a component with four options. The user is supposed to choose only one option. So if the user chooses another option, the previosluy selected ones should change to false. This is my code:


const riddles_ = [{
    id:2,
    options: ['A grandson','A daughter','A father','A nephew'],
    state_: [false,false,false,false]
    }]

function toggleToDo(o){ 
        riddles_.state_ = [false,false,false,false]
        riddles_.state_[o] = true
        console.log(riddles_.state_)
    }

    return (
        <div>
            <input type="checkbox" id="check1" checked={riddles_.state_[0]} onClick={(o)=>toggleToDo(0)}/>
            {riddles_.options[0]}
            <div className='space'></div>
            <input type="checkbox" id="check2" checked={riddles_.state_[1]} onClick={(o)=>toggleToDo(1)}/>
            {riddles_.options[1]}
            <div className='space'></div>
            <input type="checkbox" id="check3" checked={riddles_.state_[2]} onClick={(o)=>toggleToDo(2)}/>
            {riddles_.options[2]}
            <div className='space'></div>
            <input type="checkbox" id="check4" checked={riddles_.state_[3]} onClick={(o)=>toggleToDo(3)}/>
            {riddles_.options[3]}
        </div>
    )
}

When the user clicks on a checkbox, it just turns true or false, and generates no change in the others. I know the riddles_.state_ array is changing but the checkbox don´t seem to be able to take its value.

2
  • 1
    Your not re rendering the component. You need to use the useState() hook. Commented Jan 7, 2022 at 21:53
  • By the way, a better practice is to use radio button. Commented Jan 7, 2022 at 22:34

1 Answer 1

1

Its a best practice to use set method frome useState hooks when u work with state, using react functional components.

Example :

const init_state = [false,false,false,false]
const [checked_state, setCheckedState] = useState(init_state);

function toggleToDo(o){ 
        const current_state = [false,false,false,false]
        current_state[o] = true;
        setCheckedState(current_state);
    }

    return (
        <div>
            <input type="checkbox" checked={checked_state[0]} onClick={(o)=>toggleToDo(0)}/>
            option1
            <div className='space'></div>
            <input type="checkbox" checked={checked_state[1]} onClick={(o)=>toggleToDo(1)}/>
            option2
            <div className='space'></div>
            <input type="checkbox" checked={checked_state[2]} onClick={(o)=>toggleToDo(2)}/>
            option3
            <div className='space'></div>
            <input type="checkbox" checked={checked_state[3]} onClick={(o)=>toggleToDo(3)}/>
            option4
        </div>
    )
}
Sign up to request clarification or add additional context in comments.

1 Comment

I'm adding a Codesandbox with a working demo. It's doing the same thing as in Bona's answer.

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.