Skip to content
Advertisement

React conditional rendering based on other state doesn’t work

I’m new to react and I honestly can’t understand conditional rendering. I have one useState and one useReducer:

JavaScript

Following tutorials, I made a calculator with currentOperand, previousOperand, Operation and every time the currentOperand is 10, I want trivia to be “something”. I honestly don’t understand, since I don’t get any errors. I tried this:

JavaScript

and this

JavaScript

but nothing appears. Not even errors. Any help? Full code:

JavaScript

Advertisement

Answer

In your component, use useEffect() to mutate Trivia‘s value when currentOperand was mutated.

Then just use Trivia to display in return

like

JavaScript

Update [2022/09/05]

Assume your reducer works fine.

Then these code maybe can help When the currentOperand === 10, the Trivia changed

JavaScript
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement