Ejemplo 1: if else render reaccionar
render() {
const isLoggedIn = this.state.isLoggedIn;
return (
{isLoggedIn ? (
) : (
)}
);
}
Ejemplo 2: cómo usar if else dentro de jsx en react
renderElement(){
if(this.state.value == 'news')
return data ;
return null;
}
render() {
return (
{ this.renderElement() }
)
}
Ejemplo 3: reaccionar renderizado condicional nativo
function Mailbox(props) {
const unreadMessages = props.unreadMessages;
return (
Hello!
{unreadMessages.length > 0 &&
You have {unreadMessages.length} unread messages.
}
);
}
Ejemplo 4: declaración reaccionar if
render() {
const isLoggedIn = this.state.isLoggedIn;
return (
The user is {isLoggedIn ? 'currently' : 'not'} logged in.
);
}
Ejemplo 5: reacción de renderizado condicional
import React, { Component } from 'react';
// @params [] * denotes optional param (we will need to use conditional rendering for some of these)
// [type](Bulma CSS class): Hero type, focuses on the base styling
// size(Bulma CSS Class): The size of the hero, small, medium, large, etc...
// heading: The main heading
// [subheading]: The subheading if desired
// [alignment](Bulma CSS Class): Aligns the content horizontally
// This Simple HeroComponent is bases upon the following
// https://bulma.io/documentation/layout/hero/
export class HeroComponent extends Component
{
render() {
return (
// The following ternary simply applies a class if it has been specified
// Again, another ternary applying a class... blah blah blah....
{this.props.heading}
// So, to answer the question...
// The following is one way to do conditional rendering, probably the simplest and cleanest
// If this.props.subheading exists, render
{this.props.subheading && {this.props.subheading}}
)
}
}
¡Haz clic para puntuar esta entrada!
(Votos: 2 Promedio: 4)