1

I am trying to do this:

{this.state.logged ? 
     <p class="nav navbar-text hidden-xs"><span class="glyphicon glyphicon-user"></span> Bienvenido, {this.state.nombre} ({this.state.dni})</p> 
     <p class="nav navbar-text visible-xs-block"><span class="glyphicon glyphicon-user"></span> Bienvenido, {this.state.nombre}</p>
     <li class="salir"><a href="http://www.upct.es/"><span class="glyphicon glyphicon-off"></span> Salir</a></li>
     : 
     <li class="entrar"><a href="http://www.upct.es/"><span class="glyphicon glyphicon-user"></span> Entrar</a></li>
}

I tryed

{
    if (this.state.logged) {
        <p class="nav navbar-text hidden-xs"><span class="glyphicon glyphicon-user"></span> Bienvenido, {this.state.nombre} ({this.state.dni})</p> 
        <p class="nav navbar-text visible-xs-block"><span class="glyphicon glyphicon-user"></span> Bienvenido, {this.state.nombre}</p>
        <li class="salir"><a href="http://www.upct.es/"><span class="glyphicon glyphicon-off"></span> Salir</a></li>
    } else
        <li class="entrar"><a href="http://www.upct.es/"><span class="glyphicon glyphicon-user"></span> Entrar</a></li>
    }
}

But I get an error. It is not working too. Just allow me one line in the case 'true' and in the case 'false'. How could I add this three lines? Thank you.

EDIT: This is more code where you can see its locate. (In a Bootstrap Navbar) (In this code, it is shown as I resolve it, wrapping one by one).

<div class="collapse navbar-collapse">
                    <ul class="nav navbar-nav navbar-left">
                        <MenuNavItem to='/administrador/inicio' index={true} /*menuItemActive="inicio"*/>Inicio</MenuNavItem>
                        <MenuNavItem to='/administrador/nueva_incidencia' /*menuItemActive="nueva_incidencia"*/>Nueva Incidencia</MenuNavItem>
                        <MenuNavItem to='/administrador/incidencias_recibidas' /*menuItemActive="incidencias_recibidas"*/>Incidencias Recibidas</MenuNavItem>
                        <MenuNavItem to='/administrador/informes' /*menuItemActive="informes"*/>Informes</MenuNavItem>
                    </ul>

                    <ul class="nav navbar-nav navbar-right">
                        <li class="divider visible-xs-block"></li>
                        {this.state.logged === true ? <p class="nav navbar-text hidden-xs"><span class="glyphicon glyphicon-user"></span> Bienvenido, {this.state.nombre} ({this.state.dni})</p> : ''} 
                        {this.state.logged === true ? <p class="nav navbar-text visible-xs-block"><span class="glyphicon glyphicon-user"></span> Bienvenido, {this.state.nombre}</p> : ''}
                        {this.state.logged === false ? <li class="entrar"><a href="http://www.upct.es/"><span class="glyphicon glyphicon-user"></span> Entrar</a></li> : ''}
                        {this.state.logged === true ? <li class="salir"><a href="http://www.upct.es/"><span class="glyphicon glyphicon-off"></span> Salir</a></li> : ''}
                    </ul>
                </div>
0

3 Answers 3

1

I little bit changed your solution:

{this.state.logged && <p class="nav navbar-text hidden-xs"><span class="glyphicon glyphicon-user"></span> Bienvenido, {this.state.nombre} ({this.state.dni})</p>} 
{this.state.logged && <p class="nav navbar-text visible-xs-block"><span class="glyphicon glyphicon-user"></span> Bienvenido, {this.state.nombre}</p>}
{!this.state.logged && <li class="entrar"><a href="http://www.upct.es/"><span class="glyphicon glyphicon-user"></span> Entrar</a></li>}
{this.state.logged && <li class="salir"><a href="http://www.upct.es/"><span class="glyphicon glyphicon-off"></span> Salir</a></li>}
Sign up to request clarification or add additional context in comments.

6 Comments

please add more code to your question - where do you use it? what is outside of { } ?
Ok, already done. It is a Navbar, where I want that in function of one user is Logged shown X buttons or not.
oh my crystall ball can't help
so answer your own question - it may be helpful for someone
Do not worry about. It is 'solved'. Maybe not in the cleanest way, but the problem is solved. Thank you.
|
0

One way to do if to use a ternary in your code rendering and wrap the two blocks with a div, when you've several lines of xml markdown you've to wrap with a div, else it shouldn't work.

{this.state.logged === true ? 

     <div>
     <p class="nav navbar-text hidden-xs"><span class="glyphicon glyphicon-user"></span> Bienvenido, {this.state.nombre} ({this.state.dni})</p> 
     <p class="nav navbar-text visible-xs-block"><span class="glyphicon glyphicon-user"></span> Bienvenido, {this.state.nombre}</p>
     <li class="salir"><a href="http://www.upct.es/"><span class="glyphicon glyphicon-off"></span> Salir</a></li>
     </div>
     : 
     <div>
     <li class="entrar"><a href="http://www.upct.es/"><span class="glyphicon glyphicon-user"></span> Entrar</a></li>
     </div>
}

You can also replace all the 'class' by 'className'

2 Comments

I believe that you can also add a , to the end of each closing tag instead of wrapping in a div, so long as this is ultimately being included in a render that is wrapping everything in one parent tag.
Thank you. But it is not working. They loses their styles Wrapping them into a DIV. And with ',' I get error.
0

SOLVED: The solution is to wrap them one by one so they do not fail or lose your styles.

{this.state.logged === true ? <p class="nav navbar-text hidden-xs"><span class="glyphicon glyphicon-user"></span> Bienvenido, {this.state.nombre} ({this.state.dni})</p> : ''} 
{this.state.logged === true ? <p class="nav navbar-text visible-xs-block"><span class="glyphicon glyphicon-user"></span> Bienvenido, {this.state.nombre}</p> : ''}
{this.state.logged === false ? <li class="entrar"><a href="http://www.upct.es/"><span class="glyphicon glyphicon-user"></span> Entrar</a></li> : ''}
{this.state.logged === true ? <li class="salir"><a href="http://www.upct.es/"><span class="glyphicon glyphicon-off"></span> Salir</a></li> : ''}

OR:

{this.state.logged ? <p class="nav navbar-text hidden-xs"><span class="glyphicon glyphicon-user"></span> Bienvenido, {this.state.nombre} ({this.state.dni})</p> : ''} 
{this.state.logged ? <p class="nav navbar-text visible-xs-block"><span class="glyphicon glyphicon-user"></span> Bienvenido, {this.state.nombre}</p> : ''}
{this.state.logged ? '' : <li class="entrar"><a href="http://www.upct.es/"><span class="glyphicon glyphicon-user"></span> Entrar</a></li>}
{this.state.logged ? <li class="salir"><a href="http://www.upct.es/"><span class="glyphicon glyphicon-off"></span> Salir</a></li> : ''}

2 Comments

Which version of react are you using? class is not a standard prop for this library? Instead of wrapping the things into a div would be to create a specific component for each state instead of having code like this?
I'm using React ^ 15.4.2. With a plugin that allows me to use Class instead of ClassName. Yes, it would be an option, but it is not necessary in this case.

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.