Click here to Skip to main content
14,868,587 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
import React from "react";
import ReactDOM from "react-dom";
import "./styles.css";
function App() {
const check = 0; // 0, "", null, undefined
return (
<div className="App">
{ check && <span>Hello</span> }
</div>
);
}
const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);


What I have tried:

Please tell the awnser of this question. I'm getting 0 of this awnser.
Posted
Updated 2-Mar-21 1:54am
Comments
Richard MacCutchan 2-Mar-21 3:45am
   
What output do you get when you try it?

1 solution

Hi
I am not quite sure what it is you want to achieve here.
with your code you should just get a 0 (Zero) output on your page.

Check is inside the {} so it is treated as JS
as it is zero it can also be seen as a boolean value (false).
If you set it to 1 it would be seen as true.

If you want to output "0 & Hello" you need to use & amp not &&
&& is seen as a comparison in an if.
So you would need another value after the && to check against

If you want to test the "check" state you can either use an if
statement outside the return or inline the if using a ?

function App() {
  const check = 0; // 0, "", null, undefined
  let result = "";
  if (check) {
    result = <span>Hello result 1</span>;
  } else {
    result = <span>Hello result 0</span>;
  }

  return (
    <div >
      <span>Hello all</span>
      <hr />
      { check} & <span>Hello check</span>
      <hr />
      { result}
      <hr />
      { check ? <span>Hello check 1</span> : <span>Hello check 0</span>}
    </div >
  );
}
const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900