Advertisement
shopnilSS

Conditional Component

May 4th, 2021
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import React, { Component } from 'react'
  2. import Homepage from './HomePage'
  3. import LogOut from './LogOut'
  4.  
  5.  
  6. class Parent extends Component {
  7.     constructor(props) {
  8.         super(props)
  9.    
  10.         this.state = {
  11.              count : 0,
  12.              isLoggedIn: false
  13.         }
  14.     }
  15.     increase = () => {
  16.         this.setState({
  17.             count: this.state.count + 1
  18.         })
  19.     }
  20.     decrease = () => {
  21.         if(this.state.count > 0){
  22.             this.setState({
  23.                 count: this.state.count - 1
  24.             })
  25.         }else{
  26.             return
  27.         }
  28.     }
  29.     check = () => {
  30.          const element = this.state.isLoggedIn ? <Homepage/> : <LogOut/>
  31.          return element
  32.     }
  33.     render() {
  34.         return (
  35.              <div>
  36.                 <h1>Count: {this.state.count}</h1>
  37.                 <button onClick = {this.check}>Click me</button>
  38.              </div>
  39.         )
  40.     }
  41. }
  42.  
  43. export default Parent
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement