Answers for "conditional render react"

4

conditional style react

class App extends Component {
  constructor() {
    super()
    this.state = { isRed: true }
  }

  render() {
    const isRed = this.state.isRed

    return <p style={{ color: isRed ? 'red' : 'blue' }}>Example Text</p>
  }
}
Posted by: Guest on August-19-2020
4

react native conditional rendering

class Component extends React.Component {
	const a = true
	render() {
      return (
    	<Container>
          {a == true
           ? (<Button/>)
           : null
          }
        </Container>    	
	  )
	}
}

This is staying: if a == true, render a button component. 
Otherwise render null, in other words nothing.
Posted by: Guest on November-18-2020
8

ternary react

render() {
  const isLoggedIn = this.state.isLoggedIn;
  return (
    <div>
      The user is <b>{isLoggedIn ? 'currently' : 'not'}</b> logged in.    
	</div>
  );
}
Posted by: Guest on July-10-2020
1

conditional props react

// single prop
propName={ condition ? something : somethingElse }
propName={ condition && something }
// multiple props
{ ...( condition ? { ...setOfProps } : { ...setOfOtherProps })}
{ ...( condition && { ...setOfProps })}
Posted by: Guest on January-21-2021
6

react if statement

render() {
  const isLoggedIn = this.state.isLoggedIn;
  return (
    <div>
      The user is <b>{isLoggedIn ? 'currently' : 'not'}</b> logged in.    </div>
  );
}
Posted by: Guest on June-21-2020
0

conditional render react

function UserGreeting(props) {
  return <h1>Welcome back!</h1>;
}

function GuestGreeting(props) {
  return <h1>Please sign up.</h1>;
}
Posted by: Guest on July-22-2021

Browse Popular Code Answers by Language