I’m new to react and I honestly can’t understand conditional rendering. I have one useState and one useReducer:
JavaScript
x
3
1
const [Trivia, setTrivia] = useState("Nothing")
2
const [{ currentOperand, previousOperand, Operation }, dispatch] = useReducer(reducer, {})
3
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
1
16
16
1
const CheckEqual = () => {
2
if (currentOperand === 10) {
3
return (
4
<h4>
5
{setTrivia("Yes!")}
6
</h4>
7
)
8
} else {
9
return (
10
<h4>
11
{Trivia}
12
</h4>
13
)
14
}
15
}
16
and this
JavaScript
1
2
1
<div> { CheckEqual() } </div>
2
but nothing appears. Not even errors. Any help? Full code:
JavaScript
1
178
178
1
import React, { useReducer, useState, useEffect } from "react"
2
import DigitButton from "./DigitButton"
3
import OperationButton from "./OperationButton"
4
import "./styles.css"
5
6
export const actionsPossible = {
7
add_: "add",
8
operation_: "operation",
9
clear_: "clear",
10
delete_: "delete",
11
equal_: "equal",
12
}
13
function reducer(state, { type, payload }) {
14
switch (type) {
15
case actionsPossible.add_: //add
16
if (state.overwrite) {
17
return {
18
state,
19
currentOperand: payload.digit,
20
overwrite: false,
21
}
22
}
23
if (payload.digit === "." && state.currentOperand == null) {
24
return state
25
}
26
if (payload.digit === "0" && state.currentOperand === "0") {
27
return state
28
}
29
if (payload.digit === "." && state.currentOperand.includes(".")) {
30
return state
31
}
32
return {
33
state,
34
currentOperand: `${state.currentOperand || ""}${payload.digit}`,
35
}
36
case actionsPossible.clear_: //clear
37
{
38
return {}
39
}
40
case actionsPossible.operation_: //choosing operation
41
if (state.currentOperand == null && state.previousOperand == null) {
42
return state
43
}
44
if (state.currentOperand == null) {
45
return {
46
state,
47
Operation: payload.Operation,
48
}
49
}
50
if (state.previousOperand == null) {
51
return {
52
state,
53
Operation: payload.Operation,
54
previousOperand: state.currentOperand,
55
currentOperand: null,
56
}
57
}
58
59
return {
60
state,
61
previousOperand: evaluate(state),
62
currentOperand: null,
63
operation_: payload.operation_,
64
}
65
case actionsPossible.equal_: //equal
66
{
67
if (state.Operation == null || state.currentOperand == null || state.previousOperand == null) {
68
return state
69
}
70
return {
71
state,
72
overwrite: true,
73
previousOperand: null,
74
currentOperand: evaluate(state),
75
Operation: null,
76
}
77
}
78
case actionsPossible.delete_: //delete
79
{
80
if (state.overwrite) {
81
return {
82
state,
83
overwrite: false,
84
currentOperand: null
85
}
86
}
87
if (state.currentOperand == null) {
88
return state
89
}
90
if (state.currentOperand.length === 1) {
91
return {
92
state,
93
currentOperand: null
94
}
95
}
96
return {
97
state,
98
currentOperand: state.currentOperand.slice(0, -1)
99
}
100
}
101
default: return state //default case
102
}
103
}
104
function evaluate({ currentOperand, previousOperand, Operation }) {
105
const previous = parseFloat(previousOperand)
106
const current = parseFloat(currentOperand)
107
if (isNaN(previous) || isNaN(current)) return ""
108
let computation = ""
109
switch (Operation) {
110
case "+":
111
computation = previous + current
112
break
113
case "-":
114
computation = previous - current
115
break
116
case "*":
117
computation = previous * current
118
break
119
case "÷":
120
computation = previous / current
121
break
122
default:
123
return
124
}
125
return computation.toString()
126
}
127
128
function App() {
129
const [Trivia, setTrivia] = useState("")
130
const [theme, setTheme] = useState("light")
131
const [{ currentOperand, previousOperand, Operation }, dispatch] = useReducer(reducer, {})
132
133
useEffect(() => {
134
if (currentOperand === 10) {
135
setTrivia("Yes!");
136
}
137
else {
138
setTrivia("Nothing");
139
}
140
}, [currentOperand])
141
142
return (
143
<div className="calculator-grid">
144
<div className="output">
145
<div className="previous-operand">{formatOperand(previousOperand)} {Operation}</div>
146
<div className="current-operand">{formatOperand(currentOperand)}</div>
147
</div>
148
<button className="span-two" onClick={() => dispatch({ type: actionsPossible.clear_ })}>AC</button>
149
<button onClick={() => dispatch({ type: actionsPossible.delete_ })}>DEL</button>
150
<OperationButton Operation="÷" dispatch={dispatch} />
151
<DigitButton digit="1" dispatch={dispatch} />
152
<DigitButton digit="2" dispatch={dispatch} />
153
<DigitButton digit="3" dispatch={dispatch} />
154
<OperationButton Operation="*" dispatch={dispatch} />
155
<DigitButton digit="4" dispatch={dispatch} />
156
<DigitButton digit="5" dispatch={dispatch} />
157
<DigitButton digit="6" dispatch={dispatch} />
158
<OperationButton Operation="+" dispatch={dispatch} />
159
<DigitButton digit="7" dispatch={dispatch} />
160
<DigitButton digit="8" dispatch={dispatch} />
161
<DigitButton digit="9" dispatch={dispatch} />
162
<OperationButton Operation="-" dispatch={dispatch} />
163
<DigitButton digit="." dispatch={dispatch} />
164
<DigitButton digit="0" dispatch={dispatch} />
165
<button className="span-two" onClick={() => dispatch({ type: actionsPossible.equal_ })}>=</button>
166
{/*----------------------------------TRIVIA--------------------------------*/}
167
<div className="count">
168
<h1>Random Trivia</h1>
169
<h2>(it will randomly pop up the more you use the calculator)</h2>
170
<h4>{Trivia}</h4>
171
</div>
172
</div>
173
);
174
}
175
176
export default App;
177
178
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
1
16
16
1
const [Trivia, setTrivia] = useState("Nothing")
2
const [{ currentOperand, previousOperand, Operation }, dispatch] = useReducer(reducer, {})
3
4
useEffect(()=>{
5
if (currentOperand === 10) {
6
setTrivia("Yes!");
7
}
8
else {
9
setTrivia("Nothing");
10
}
11
}, [currentOperand])
12
13
return <>
14
{Trivia}
15
</>
16
Update [2022/09/05]
Assume your reducer works fine.
Then these code maybe can help When the currentOperand === 10, the Trivia changed
JavaScript
1
37
37
1
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/18.2.0/umd/react.production.min.js"></script>
2
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/18.2.0/umd/react-dom.production.min.js"></script>
3
<script src="https://unpkg.com/babel-standalone@6/babel.min.js"></script>
4
5
<div id="root"></div>
6
7
<script type="text/babel">
8
function App() {
9
const [Trivia, setTrivia] = React.useState("Nothing")
10
const [currentOperand, setCurrentOperand] = React.useState(0);
11
12
React.useEffect(()=>{
13
if (currentOperand === 10) {
14
setTrivia("Yes!");
15
}
16
else {
17
setTrivia("Nothing");
18
}
19
}, [currentOperand])
20
21
const handleClick = () =>{
22
setCurrentOperand(currentOperand+1)
23
}
24
25
return <div>
26
<button onClick={handleClick}>Add currentOperand</button>
27
<div>{Trivia}</div>
28
<div>{currentOperand}</div>
29
</div>
30
}
31
</script>
32
33
<script type="text/babel">
34
ReactDOM.render(
35
<App></App>
36
, document.getElementById("root"));
37
</script>