Skip to content

Commit f5a1116

Browse files
committed
Avoid All Possible Unnecessary Re-renders
1 parent 7b68e06 commit f5a1116

4 files changed

Lines changed: 52 additions & 33 deletions

File tree

src/App.js

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import React, { Component } from 'react';
22
import axios from 'axios';
33
import { BrowserRouter, Route } from 'react-router-dom';
4+
import { BeatLoader } from 'react-spinners';
45

56
import './App.css';
67

@@ -12,7 +13,7 @@ class App extends Component {
1213
tops: '',
1314
data: '',
1415
searchResult: '',
15-
singleData: ''
16+
loading: true
1617
}
1718

1819
getData() {
@@ -29,7 +30,7 @@ class App extends Component {
2930
const data = this.getData();
3031
const tops = this.topData();
3132
const getAllData = await Promise.all([data, tops]);
32-
this.setState({data: getAllData[0].data, tops: getAllData[1].data});
33+
this.setState({data: getAllData[0].data, tops: getAllData[1].data, loading: false});
3334
}
3435

3536
componentDidMount() {
@@ -45,8 +46,14 @@ class App extends Component {
4546

4647
render() {
4748
const { searchAItem } = this;
48-
const { tops, searchResult, data } = this.state;
49-
49+
const { tops, searchResult, data, loading } = this.state;
50+
51+
if(loading) {
52+
return <div className="loader">
53+
<BeatLoader color={'#333'} />
54+
</div>;
55+
}
56+
5057
return (
5158
<BrowserRouter>
5259
<div className="App">

src/components/Card.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import React from 'react';
22
import { Link } from 'react-router-dom';
33

44
export default({id, title, style = {}}) => (
5-
<div key={id} className="card c-card" style={style}>
5+
<div key={id} className="card c-card" style={style} log={console.log('Card')}>
66
<Link to={`/${id}`}>
77
<h4>{title}</h4>
88
</Link>

src/components/HeaderContent.js

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,26 @@
1-
import React from 'react';
1+
import React, { PureComponent } from 'react';
22

33
import NavBar from './NavBar';
44
import SocialShare from './SocialShare';
55

6-
export default({title, description}) => (
7-
<header className="header-area">
8-
<div className="header">
9-
<NavBar />
10-
<div className="intro">
11-
<h1>{title}</h1>
12-
<p>{description}</p>
13-
<div className="print no-print">
14-
<button className="btn btn-print" onClick={() => window.print()}>প্রিন্ট করুন</button>
6+
export default class HeaderContent extends PureComponent {
7+
render() {
8+
const {title, description} = this.props;
9+
10+
return (
11+
<header className="header-area">
12+
<div className="header">
13+
<NavBar />
14+
<div className="intro">
15+
<h1>{title}</h1>
16+
<p>{description}</p>
17+
<div className="print no-print">
18+
<button className="btn btn-print" onClick={() => window.print()}>প্রিন্ট করুন</button>
19+
</div>
20+
<SocialShare title={title} description={description} />
21+
</div>
1522
</div>
16-
<SocialShare title={title} description={description} />
17-
</div>
18-
</div>
19-
</header>
20-
)
23+
</header>
24+
)
25+
}
26+
}

src/components/HeaderHome.js

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,22 @@
1-
import React from 'react';
1+
import React, { PureComponent } from 'react';
22

33
import NavBar from './NavBar';
44

5-
export default({searchAItem}) => (
6-
<header className="header-area">
7-
<div className="header">
8-
<NavBar />
9-
<div className="intro">
10-
<h1>ডেভেলপার চিটশিট</h1>
11-
<input onChange={(e) => searchAItem(e.target.value)} placeholder="কিসের উপর চিটশিট চাচ্ছেন?" />
12-
<p>বাংলা চিটশিটের ভান্ডার। নিজের মাতৃভাষায় চিটশিটের ভান্ডার সমৃদ্ধ করতে আপনিও যোগ দিন</p>
13-
</div>
14-
</div>
15-
</header>
16-
)
5+
export default class HeaderHome extends PureComponent {
6+
render() {
7+
const { searchAItem } = this.props;
8+
9+
return (
10+
<header className="header-area">
11+
<div className="header">
12+
<NavBar />
13+
<div className="intro">
14+
<h1>ডেভেলপার চিটশিট</h1>
15+
<input onChange={(e) => searchAItem(e.target.value)} placeholder="কিসের উপর চিটশিট চাচ্ছেন?" />
16+
<p>বাংলা চিটশিটের ভান্ডার। নিজের মাতৃভাষায় চিটশিটের ভান্ডার সমৃদ্ধ করতে আপনিও যোগ দিন</p>
17+
</div>
18+
</div>
19+
</header>
20+
)
21+
}
22+
}

0 commit comments

Comments
 (0)