You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This is kind of a subtle React philosophy thing, but you should generally be suspicious if your code ever has to touch the real DOM. React is designed to handle all DOM updates for you, so if you're ever doing a querySelector or directly changing classNames etc that's usually something to avoid.
Remember your component re-runs whenever state changes, which means you can put conditions and dynamic stuff into your JSX to determine e.g. what classNames elements should have. You just need to have state values that let you know what to render.
E.g.
{shuffledChoices.map(choice=>(<button// if we have chosen this button, then if the answer is correct make it green otherwise redclassName={selectedChoice===choice&&(selectedChoice===correct_answer ? "green" : "red")}onClick={()=>{setSelectedChoice(choice);// store the selected choice in statesetIndex(index+1)}}>{choice}</button>))}
Generally React updates are a lot easier to manage if you do as little logic as possible in event handlers and try to just make them simple state updates. You can then use the updated state in your render to determine what the page should look like.
The text was updated successfully, but these errors were encountered:
week10-KJ/src/components/PlayGame/Quiz.js
Lines 30 to 38 in fb29040
This is kind of a subtle React philosophy thing, but you should generally be suspicious if your code ever has to touch the real DOM. React is designed to handle all DOM updates for you, so if you're ever doing a
querySelector
or directly changing classNames etc that's usually something to avoid.Remember your component re-runs whenever state changes, which means you can put conditions and dynamic stuff into your JSX to determine e.g. what classNames elements should have. You just need to have state values that let you know what to render.
E.g.
Generally React updates are a lot easier to manage if you do as little logic as possible in event handlers and try to just make them simple state updates. You can then use the updated state in your render to determine what the page should look like.
The text was updated successfully, but these errors were encountered: