While using AND operator to evaluate the expression in line 6,
1. export default function App() {
2. var isDone = false;
3. const strikethrough = { textDecoration: "line-through" };
4. return (
5. <div className="App">
6. <h1 style={isDone && strikethrough}>Hello CodeSandbox</h1>
7. </div>
8. );
9. }
when isDone = false
,
I am getting an error as
'The
style
prop expects a mapping from style properties to values, not a string. For example, style={{marginRight: spacing + 'em'}} when using JSX.
when isDone = true
, things are working fine.
Somehow the null is not working and in order to check that, I used the following
<h1 style={}>Hello CodeSandbox</h1>
This gives an error that 'JSX attributes must only be assigned a non-empty expression'
I am learning ReactJs from an online course. What's happening here?
Hello CodeSandbox
, and this works fine but why doesn't the && operator work as intended in this case – sankha Jul 03 '20 at 11:49