refactor - use of PureComponents to avoid useless re-rendering

This commit is contained in:
Sam 2019-01-09 12:07:57 +01:00
parent 3fdbca0404
commit 18df5b994a
4 changed files with 312 additions and 303 deletions

View File

@ -320,7 +320,7 @@
"react/no-unknown-property": "error", "react/no-unknown-property": "error",
"react/no-unused-prop-types": "off", "react/no-unused-prop-types": "off",
"react/prefer-es6-class": "error", "react/prefer-es6-class": "error",
"react/prefer-stateless-function": "error", "react/prefer-stateless-function": [1, { "ignorePureComponents": true }],
"react/prop-types": "off", "react/prop-types": "off",
"react/react-in-jsx-scope": "error", "react/react-in-jsx-scope": "error",
"react/react-default-props": "off", "react/react-default-props": "off",

View File

@ -1,7 +1,8 @@
import React from 'react' import React from 'react'
export default function ActivitiesList (props) { export default class ActivitiesFilter extends React.PureComponent {
const { loadActivities, sports, updateParams } = props render() {
const { loadActivities, sports, updateParams } = this.props
return ( return (
<div className="card"> <div className="card">
<div className="card-body activity-filter"> <div className="card-body activity-filter">
@ -174,3 +175,4 @@ export default function ActivitiesList (props) {
</div> </div>
) )
} }
}

View File

@ -4,8 +4,9 @@ import { Link } from 'react-router-dom'
import { getDateWithTZ } from '../../utils' import { getDateWithTZ } from '../../utils'
export default function ActivitiesList (props) { export default class ActivitiesList extends React.PureComponent {
const { activities, sports, user } = props render() {
const { activities, sports, user } = this.props
return ( return (
<div className="card activity-card"> <div className="card activity-card">
<div className="card-body"> <div className="card-body">
@ -59,3 +60,4 @@ export default function ActivitiesList (props) {
</div> </div>
) )
} }
}

View File

@ -4,8 +4,9 @@ import { Link } from 'react-router-dom'
import { apiUrl } from '../../utils' import { apiUrl } from '../../utils'
class NavBar extends React.PureComponent {
function NavBar(props) { render() {
const { user } = this.props
return ( return (
<header> <header>
<nav className="navbar navbar-expand-lg navbar-light bg-light"> <nav className="navbar navbar-expand-lg navbar-light bg-light">
@ -22,7 +23,10 @@ function NavBar(props) {
> >
<span className="navbar-toggler-icon" /> <span className="navbar-toggler-icon" />
</button> </button>
<div className="collapse navbar-collapse" id="navbarSupportedContent"> <div
className="collapse navbar-collapse"
id="navbarSupportedContent"
>
<ul className="navbar-nav mr-auto"> <ul className="navbar-nav mr-auto">
<li className="nav-item"> <li className="nav-item">
<Link <Link
@ -34,7 +38,7 @@ function NavBar(props) {
Dashboard Dashboard
</Link> </Link>
</li> </li>
{props.user.isAuthenticated && ( {user.isAuthenticated && (
<li className="nav-item"> <li className="nav-item">
<Link <Link
className="nav-link" className="nav-link"
@ -46,7 +50,7 @@ function NavBar(props) {
</Link> </Link>
</li> </li>
)} )}
{props.user.isAuthenticated && ( {user.isAuthenticated && (
<li className="nav-item"> <li className="nav-item">
<Link <Link
className="nav-link" className="nav-link"
@ -58,7 +62,7 @@ function NavBar(props) {
</Link> </Link>
</li> </li>
)} )}
{/* {props.user.admin && ( */} {/* {user.admin && ( */}
{/* <li className="nav-item"> */} {/* <li className="nav-item"> */}
{/* <Link */} {/* <Link */}
{/* className="nav-link" */} {/* className="nav-link" */}
@ -72,7 +76,7 @@ function NavBar(props) {
{/* )} */} {/* )} */}
</ul> </ul>
<ul className="navbar-nav flex-row ml-md-auto d-none d-md-flex"> <ul className="navbar-nav flex-row ml-md-auto d-none d-md-flex">
{!props.user.isAuthenticated && ( {!user.isAuthenticated && (
<li className="nav-item"> <li className="nav-item">
<Link <Link
className="nav-link" className="nav-link"
@ -84,7 +88,7 @@ function NavBar(props) {
</Link> </Link>
</li> </li>
)} )}
{!props.user.isAuthenticated && ( {!user.isAuthenticated && (
<li className="nav-item"> <li className="nav-item">
<Link <Link
className="nav-link" className="nav-link"
@ -96,15 +100,15 @@ function NavBar(props) {
</Link> </Link>
</li> </li>
)} )}
{props.user.picture === true && ( {user.picture === true && (
<img <img
alt="Avatar" alt="Avatar"
src={`${apiUrl}users/${props.user.id}/picture` + src={`${apiUrl}users/${user.id}/picture` +
`?${Date.now()}`} `?${Date.now()}`}
className="img-fluid App-nav-profile-img" className="img-fluid App-nav-profile-img"
/> />
)} )}
{props.user.isAuthenticated && ( {user.isAuthenticated && (
<li className="nav-item"> <li className="nav-item">
<Link <Link
className="nav-link" className="nav-link"
@ -112,11 +116,11 @@ function NavBar(props) {
pathname: '/profile', pathname: '/profile',
}} }}
> >
{props.user.username} {user.username}
</Link> </Link>
</li> </li>
)} )}
{props.user.isAuthenticated && ( {user.isAuthenticated && (
<li className="nav-item"> <li className="nav-item">
<Link <Link
className="nav-link" className="nav-link"
@ -135,6 +139,7 @@ function NavBar(props) {
</header> </header>
) )
} }
}
export default connect( export default connect(
state => ({ state => ({