Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

261
Views
React js | Conditional rendering

I have a problem with conditional rendering in reactjs.

Problem

If an environment is prod, then <Sentry.ErrorBoundary> tag should be loaded.

Is there a better way instead of using if with sentrytag and else without sentrytag?

Code:
ReactDOM.render(
    <Provider store={store}>
        {environment == 'PROD' && <Sentry.ErrorBoundary> }
            <Experiment>
                <Root />
            </Experiment>
            {environment == 'PROD' && </Sentry.ErrorBoundary> }
    </Provider>, document.getElementById('root'));
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Is there a better way instead of using if with sentrytag and else without sentrytag

Yea you could define a variable with the 'wrapper' element, or an 'empty' React.Fragment if the element isn't needed:

const wrapper = (environment === 'PROD') ? Sentry.ErrorBoundary : React.Fragment;

<Provider store={store}>
    <wrapper>
        <Experiment>
            <Root />
        </Experiment>
    </wrapper>
</Provider>

Small working demo, just to give the idea.

The Error component is the Sentry.ErrorBoundary in OP's example.

Press the button to toggle the 'error' state to enable/disable the wrapper.

const Error = (props) => (
    <div>
        <h1>{'Omg; error!'}</h1>
        {props.children}
    </div>
);

const Example = () => {

    const [ err, setErr ] = React.useState(true);
    const Wrapper = (err) ? Error : React.Fragment;
    
    return (
        <div>
            <Wrapper>
                Hello!
            </Wrapper>
            <br /><br /><br />
            <button onClick={() => setErr(!err)}>Toggle</button>
        </div>
    )
}

ReactDOM.render(<Example />, document.getElementById("app"));
<script src="https://unpkg.com/react@18/umd/react.production.min.js"></script>
<script src="https://unpkg.com/react-dom@18/umd/react-dom.production.min.js"></script>

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

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!