Skip to content

Commit

Permalink
Working on memoization
Browse files Browse the repository at this point in the history
  • Loading branch information
metamn committed Aug 27, 2019
1 parent 621ff3f commit 3c2ffa4
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 12 deletions.
4 changes: 2 additions & 2 deletions src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ const App = () => {
<Meta />
<Reset />
<TypographicGrid
displayVerticalRhytm={true}
displayHorizontalRhytm={true}
displayVerticalRhytm={false}
displayHorizontalRhytm={false}
/>
<Router>
<Switch>
Expand Down
9 changes: 5 additions & 4 deletions src/components/LoadingData/LoadingData.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,14 @@ const LoadingData = props => {
<Section className="LoadingData" title="Loading Data" displayTitle={true}>
<ul>
<li>
<Link to="/loading-data/api-axios-placeholder-svg">
Loading data from an API with Axios using an SVG Placeholder
<Link to="/loading-data/api-axios">
Loading data from an API with Axios
</Link>
</li>

<li>
<Link to="/loading-data/api-axios">
Loading data from an API with Axios
<Link to="/loading-data/api-axios-placeholder-svg">
Loading data from an API with Axios using an SVG Placeholder
</Link>
</li>
<li>
Expand Down
12 changes: 10 additions & 2 deletions src/components/LoadingDataApiAxios/LoadingDataApiAxios.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React from "react";
import React, { useMemo } from "react";
import PropTypes from "prop-types";
import styled from "styled-components";

Expand Down Expand Up @@ -42,6 +42,7 @@ const Article = styled(_Article)(props => ({
* Generates a text placeholder for articles
*/
const ArticlesPlaceholder = props => {
console.log("ArticlesPlaceholder");
/**
* Loads the placeholder
*/
Expand Down Expand Up @@ -72,11 +73,18 @@ const Articles = props => {
*/
const { placeholder } = props;

/**
* Creates the placeholder
*/
const articlesPlaceholder = useMemo(() => ArticlesPlaceholder(placeholder), [
placeholder
]);

/**
* Loads the data
*/
const { data } = useDataAPI(
ArticlesPlaceholder(placeholder),
articlesPlaceholder,
"http://hn.algolia.com/api/v1/search?query=redux",
"hits"
);
Expand Down
55 changes: 55 additions & 0 deletions src/components/Memoization/Memoization.stories.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,61 @@ Memoizing them brings in the following results:

It seems they all worth memoized in this context. However this feature needs more reasearch.

## Caching functions

By definition `useMemo(() => memoizedValue(a, b), [a, b])` :

> Will only recompute the memoized value when one of the dependencies has changed. This optimization helps to avoid expensive calculations on every render.
In other words:

> useMemo allows you to memoize the results of a function, and will return that result until an array of dependencies change.
Again one could ask: why don't wrap every function into a `useMemo()`? The answer is:

> Don’t do anything there that you wouldn’t normally do while rendering. For example, side effects belong in useEffect, not useMemo.
In other words: just expensive calculations, no business logic. And hooks are business logic.

### Example

In the `LoadingDataApiAxios` component while the data is loading a placeholder is displayed:

```
/**
* Loads the data
*/
const { data } = useDataAPI(
ArticlesPlaceholder(placeholder),
"http://hn.algolia.com/api/v1/search?query=redux",
"hits"
);
```

The `placeholder` prop is constant &mdash; is defined as a proptype and never gets touched. That makes the `ArticlesPlaceholder` function ideal to be wrapped into a `useMemo`:

```
/**
* Creates the placeholder
*/
const articlesPlaceholder = useMemo(() => ArticlesPlaceholder(placeholder), [
placeholder
]);
/**
* Loads the data
*/
const { data } = useDataAPI(
articlesPlaceholder,
"http://hn.algolia.com/api/v1/search?query=redux",
"hits"
);
```

The number of re-renders before `useMemo` was three; now is a single one.

## Resources

- [React v16.6.0: lazy, memo and contextType](https://reactjs.org/blog/2018/10/23/react-v-16-6.html)
- [React: Optimize Components with React.memo, useMemo, and useCallback](https://headway.io/blog/react-optimize-components-memo-usememo-usecallback/)
- [Hooks API Reference](https://reactjs.org/docs/hooks-reference.html)
2 changes: 0 additions & 2 deletions src/hooks/useData.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,6 @@ const useLoadMore = (fetchMore, data, filter, variables) => {
*
*/
const useData = (defaultValues, query, filter, variables = {}) => {
console.log("useData");

/**
* Queries the database
*/
Expand Down
2 changes: 0 additions & 2 deletions src/hooks/useDataAPI.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@ import useDataApi from "use-data-api";
* @link https://github.com/the-road-to-learn-react/react-hooks-introduction/tree/master/src/useDataApiHook-external-example
*/
const useDataAPI = (defaultValues, query, filter) => {
console.log("useDataAPI");

/**
* Queries the database
*/
Expand Down

0 comments on commit 3c2ffa4

Please sign in to comment.