Skip to content

Commit 52f3ff3

Browse files
rolandszokesolkimicreb
authored andcommitted
fix(displayname): add displayName before React.memo
1 parent 7a0346f commit 52f3ff3

File tree

6 files changed

+64
-45
lines changed

6 files changed

+64
-45
lines changed

.eslintrc.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
"react/prefer-stateless-function": "off",
1515
"react/destructuring-assignment": "off",
1616
"react/state-in-constructor": "off",
17+
"react/jsx-props-no-spreading": "off",
1718
"react/prop-types": "off"
1819
},
1920
"globals": {

examples/beer-finder/src/App.jsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import React from "react";
2-
import NavBar from "./NavBar";
3-
import BeerList from "./BeerList";
1+
import React from 'react';
2+
import NavBar from './NavBar';
3+
import BeerList from './BeerList';
44

55
// if a component does not use any store, it doesn't have to be wrapped with view()
66
// it is safer to wrap everything with view() until you get more comfortable with Easy State

examples/beer-finder/src/Beer.jsx

Lines changed: 39 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,42 @@
1-
import React from "react";
2-
import { view, store } from "react-easy-state";
3-
import Card from "@material-ui/core/Card";
4-
import CardMedia from "@material-ui/core/CardMedia";
5-
import CardContent from "@material-ui/core/CardContent";
1+
import React from 'react';
2+
import { view, store } from 'react-easy-state';
3+
import Card from '@material-ui/core/Card';
4+
import CardMedia from '@material-ui/core/CardMedia';
5+
import CardContent from '@material-ui/core/CardContent';
66

77
// this is re-rendered whenever the relevant parts of the used data stores change
8-
export default view(
9-
({ name, description, image_url: imageUrl, food_pairing: foodPairing }) => {
10-
const beer = store({ details: false });
8+
const Beer = ({
9+
name,
10+
description,
11+
image_url: imageUrl,
12+
food_pairing: foodPairing,
13+
}) => {
14+
const beer = store({ details: false });
1115

12-
return (
13-
<Card onClick={() => (beer.details = !beer.details)} className="beer">
14-
{!beer.details && <CardMedia image={imageUrl} className="media" />}
15-
<CardContent>
16-
<h3>{name}</h3>
17-
{beer.details ? (
18-
<p>{description}</p>
19-
) : (
20-
<ul>
21-
{foodPairing.map(food => (
22-
<li key={food}>{food}</li>
23-
))}
24-
</ul>
25-
)}
26-
</CardContent>
27-
</Card>
28-
);
29-
}
30-
);
16+
return (
17+
<Card
18+
onClick={() => {
19+
beer.details = !beer.details;
20+
}}
21+
className="beer"
22+
>
23+
{!beer.details && (
24+
<CardMedia image={imageUrl} className="media" />
25+
)}
26+
<CardContent>
27+
<h3>{name}</h3>
28+
{beer.details ? (
29+
<p>{description}</p>
30+
) : (
31+
<ul>
32+
{foodPairing.map(food => (
33+
<li key={food}>{food}</li>
34+
))}
35+
</ul>
36+
)}
37+
</CardContent>
38+
</Card>
39+
);
40+
};
41+
42+
export default view(Beer);
Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
1-
import React from "react";
2-
import { view } from "react-easy-state";
3-
import appStore from "./appStore";
4-
import Beer from "./Beer";
1+
import React from 'react';
2+
import { view } from 'react-easy-state';
3+
import appStore from './appStore';
4+
import Beer from './Beer';
55

66
// this is re-rendered whenever the relevant parts of the used data stores change
7-
export default view(() => (
7+
const BeerList = () => (
88
<div className="beerlist">
99
{!appStore.beers.length ? (
1010
<h3>No matching beers found!</h3>
1111
) : (
1212
appStore.beers.map(beer => <Beer key={beer.name} {...beer} />)
1313
)}
1414
</div>
15-
));
15+
);
16+
17+
export default view(BeerList);
Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
import React from "react";
2-
import { view } from "react-easy-state";
3-
import SearchBar from "material-ui-search-bar";
4-
import LinearProgress from "@material-ui/core/LinearProgress";
5-
import appStore from "./appStore";
1+
import React from 'react';
2+
import { view } from 'react-easy-state';
3+
import SearchBar from 'material-ui-search-bar';
4+
import LinearProgress from '@material-ui/core/LinearProgress';
5+
import appStore from './appStore';
66

77
// this is re-rendered whenever the relevant parts of the used data stores change
8-
export default view(() => (
8+
const NavBar = () => (
99
<div className="searchbar">
1010
<SearchBar
1111
onRequestSearch={appStore.fetchBeers}
@@ -14,4 +14,6 @@ export default view(() => (
1414
/>
1515
{appStore.isLoading && <LinearProgress />}
1616
</div>
17-
));
17+
);
18+
19+
export default view(NavBar);

src/view.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ export function view(Comp) {
4242

4343
if (isStatelessComp && hasHooks) {
4444
// use a hook based reactive wrapper when we can
45-
ReactiveComp = memo(props => {
45+
ReactiveComp = props => {
4646
// use a dummy setState to update the component
4747
const [, setState] = useState();
4848
const triggerRender = useCallback(() => setState({}), []);
@@ -73,7 +73,9 @@ export function view(Comp) {
7373
} finally {
7474
isInsideFunctionComponent = false;
7575
}
76-
});
76+
};
77+
ReactiveComp.displayName = Comp.displayName || Comp.name;
78+
ReactiveComp = memo(ReactiveComp);
7779
} else {
7880
const BaseComp = isStatelessComp ? Component : Comp;
7981
// a HOC which overwrites render, shouldComponentUpdate and componentWillUnmount

0 commit comments

Comments
 (0)