Skip to main content

React Conditional Rendering

 Botton.js:

import React from 'react';

class Button extends React.Component{
    render(){
        const {change, locale, show} = this.props;
        return(
            <>
            <button type='button' onClick={() =>change(locale)}>
                {locale === 'bn-BD' ? "change clock": "গড়ি পরিবর্তন করুন"}
            </button>
            {show && <p>Hello</p>}

            </>
           
        );
    }
}

export default Button;

Clock.js:

import React from 'react';
import Button from './Button';

class Clock extends React.Component {
  state = {date: new Date(),locale: 'bn-BD'};
   
    componentDidMount(){
        this.clockTimer = setInterval(() =>this.tick(), 1000);
    }

    componentWillUnmount(){
        clearInterval(this.clockTimer);
    }

    handelClick = (locale) => {
        this.setState({
          locale,
        });
    };

    tick(){
      this.setState({
          date: new Date(),
      });
  }

  render() {
    const {date,locale}= this.state;
    // let button;
    // if(locale==='bn-BD'){
    //   button = (
    //     <Button change={this.handelClick} locale= 'en-US'>Click Me</Button>
    //   );
    // }else {
    //   button = (
    //     <Button change={this.handelClick} locale='bn-BD'>Click Me</Button>
    //   );
    // }
    return(
      <div>
        <h1 className='heading'>
        <span className='text'>{date.toLocaleTimeString(locale)}</span>
      </h1>
      {/* {button} */}
      {locale === 'bn-BD' ? (<Button change={this.handelClick} locale= 'en-US' show={false}/>):(<Button change={this.handelClick} locale= 'bn-BD' show/>)}
      </div>
    );
  }
}

export default Clock;

Comments

Popular posts from this blog

feture

Use  <nav>,<figure>,<ifrem>,<samp>,<var> Tag < html lang = "en" > < head >   < meta charset = "UTF-8" >   < meta http-equiv = "X-UA-Compatible" content = "IE=edge" >   < meta name = "viewport" content = "width=device-width, initial-scale=1.0" >   < style >     header {       text-align : center ;     }     body {       background-color : rgb ( 196 , 152 , 72 );     }     .agp {       width : 100% ;       height : 250px ;    }     .fcss {     border : 3px solid red ;     border-radius : 5px ;     padding : 5px ;     margin : 2px ;     width : 50% ;    }     .fs {       text-align : center ;     }     .t1 , .t2 , .t3 , .t4 {       background-colo...
Use CSS variable (form and list style)  If you want icon like -list ..Go to this wed -site : https://fontawesome.com/v5.15/icons?d=gallery&p=2&q=tick And attest HTML <header> this link : <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0-beta3/css/all.min.css" integrity="sha512-Fo3rlrZj/k7ujTnHg4CGR2D7kSs0v4LLanw2qksYuRlEzO+tcaEPQogQ0KaoGN26/zrn20ImR1DfuLWnOo7aBA==" crossorigin="anonymous" referrerpolicy="no-referrer" /> This is CSS sheet /* form style */ .text-are12 {     resize : none ;     border : 3px solid rgb ( 71 , 3 , 3 );     border-radius : 5px ;     margin : 10px ; } input [ type = "submit" ], input [ type = "reset" ]{     width : 90px ;     border : none ;     margin : 10px ;     border-radius : 5px ;     padding : 5px ;     background-color : olivedrab ;     color : white ;     cursor : pointer...