Skip to content

Commit

Permalink
React Event Handling & Control Re Rendering 😍
Browse files Browse the repository at this point in the history
  • Loading branch information
SOURAV-ROY committed Aug 5, 2021
1 parent 81beabc commit 4f868ed
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 5 deletions.
6 changes: 4 additions & 2 deletions src/App.js
Expand Up @@ -2,11 +2,13 @@ import './App.css';
import Clock from "./components/Clock";

function App() {
console.log('App component render');
return (
<div className="App">
{/*<header className="App-header">*/}
<h1>Hello Sourav</h1>
<Clock local='bn-BD'/>
<h1>Hello Sourav</h1>
{/*<Clock local='bn-BD'/>*/}
<Clock/>
{/*</header>*/}
</div>
);
Expand Down
28 changes: 28 additions & 0 deletions src/components/Button.js
@@ -0,0 +1,28 @@
import React, {Component} from 'react';

class Button extends Component {
// Should not use *************************
shouldComponentUpdate(nextProps, nextState, nextContext) {
const {change: currentChange, local: currentLocal} = this.props;
const {change: nextChange, local: nextLocal} = nextProps;

return !(currentChange === nextChange && currentLocal === nextLocal);
// return currentChange !== nextChange;
// if (currentChange === nextChange && currentLocal === nextLocal) {
// return false;
// }
// return true;
}

render() {
console.log('Button component render')
const {change, local} = this.props;
return (
<div>
<button type='button' onClick={() => change(local)}>Click Here</button>
</div>
);
}
}

export default Button;
36 changes: 33 additions & 3 deletions src/components/Clock.js
@@ -1,11 +1,20 @@
import React, {Component} from 'react'
import Button from './Button';

class Clock extends Component {
// constructor(props) {
// super(props);
// this.state = {date: new Date()};
// this.state = {
// date: new Date(),
// local: 'bn-BD'
// };
// this.handleClick = this.handleClick.bind(this);
// }
state = {date: new Date()};

state = {
date: new Date(),
local: 'bn-BD'
};

componentDidMount() {
this.clockTimer = setInterval(() => {
Expand All @@ -20,20 +29,41 @@ class Clock extends Component {
clearInterval(this.clockTimer);
}

// handleClick = (event) => {
handleClick = (local) => {
// event.preventDefault();
// console.log(this);
this.setState({
// local: 'en-US'
local
});
console.log('The button was clicked by handleClick');
}

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

render() {
console.log('Clock component render');
const {date, local} = this.state;
// const {local} = this.props;
return (
<div>
<h1 className='heading'>
<span className='text'>
{/*{new Date().toLocaleString(this.props.local)}*/}
{this.state.date.toLocaleString(this.props.local)}
{/*{this.state.date.toLocaleString(this.props.local)}*/}
{/*{date.toLocaleString('bn-BD')}*/}
{date.toLocaleString(local)}
</span>
<p>{this.props.local} Bangladesh</p>
{/*<button onClick={this.handleClick}>Click Here</button>*/}
{/*<button onClick={this.handleClick.bind(this, 'en-US')}>Click Here</button>*/}
{/*<button onClick={() => this.handleClick('en-US')}>Click Here</button>*/}
<Button change={this.handleClick} local='en-US'>Click Here</Button>
</h1>
</div>
)
Expand Down

0 comments on commit 4f868ed

Please sign in to comment.