Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

262
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda