1

I have a .jsx file, with:

<p_grid>
  <RangeSpan>
    Currently, there are {" "}
    <b>{Math.round(house.records * 100) / 100} houses</b> found.
  </RangeSpan>
</p_grid>

this works fine, but now I want to interrogate house.source and display different things, something like:

if ({house.source}=="X")
{
Currently, there are {" "} <b>{Math.round(house.records * 100) / 100} houses</b> found.
}
else
{
different calculation and text.
}

I can display house.source OK but cannot figure out how to do the if statement. Completely new to this so any help is appreciated (I tried using ternary operator but don't know how to combine it with {} and "" or even of it is OK to use in this context).

1

4 Answers 4

3

class TodoApp extends React.Component {
  constructor(props) {
    super(props)
    this.state = {
        shouldShow: true
    }
  }
  
  toggle() {
    this.setState(state => ({
       shouldShow: !state.shouldShow
    }));
  }
  
  render() {
    return (
      <div>
        {
          this.state.shouldShow
            ? (<p>yess, you should</p>)
            : (<p>no, you shouldn't</p>)
          }
          <button onClick={this.toggle.bind(this)}>toggle</button>
      </div>
    )
  }
}

ReactDOM.render(<TodoApp />, document.querySelector("#app"))
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>

<div id="app"></div>

This is a basic example

Sign up to request clarification or add additional context in comments.

Comments

2

You can't use an if/else statement directly in JSX, but you can do something like this:

<p_grid>
  <RangeSpan>
    {
        house.source === "X"
        && (<>Currently, there are {" "} <b>{Math.round(house.records * 100) / 100} houses</b> found.</>)
    }
    {
        house.source !== "X"
        && (<>different calculation and text.</>)
    }
  </RangeSpan>
</p_grid>

This can also be expressed as a ternary, as can be seen in TKol's answer.

If it is more involved than that, you can move the calculation higher up in the render method (or functional component) outside of the actual JSX return statement, or even encapsulate the functionality into its own component (Prebiusta's answer is a good example of this approach).

1 Comment

Exactly what I needed. Thanks!
2

This is the official documentation for conditional rendering.

https://reactjs.org/docs/conditional-rendering.html

I would do something like this

class LoginControl extends React.Component {
  render() {
    let output = <p>Some other calculations</p>;

    if (house.source) {
      output = (
        <p>
          Currently, there are{" "}
          <b>{Math.round(house.records * 100) / 100} houses</b> found
        </p>
      );
    }

    return <div>{output}</div>;
  }
}

Comments

1
You cannot use if condition in JSX tags. (Have a look [here][1] for reason)   However, you can use terinary operator in JSX tags.

You can also run a function in JSX tags where in that function you can use if condition before return statement.

Have a look [here][2] for Code snippets
                
        

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.