Skip to content

Commit

Permalink
chore: code review
Browse files Browse the repository at this point in the history
  • Loading branch information
satazor committed Feb 27, 2020
1 parent f50b88a commit 67136e0
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 34 deletions.
File renamed without changes.
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -182,13 +182,13 @@ Sets up a `Page` component with the ability to specify which layout tree to use.

Type: `ReactElement` or `function`

In simple cases, you may defined a "static" layout tree, like so:
In simple cases, you may define a "static" layout tree, like so:

```js
export default withLayout(<PrimaryLayout variant="light" />)(Home);
```

However, you might be having external props, component state or other mutations influencing the layout tree. In those cases, you may pass a function that maps **layout state** into a tree, with the following signature: `(layoutState) => <ReactElement>`. Here's an example:
However, you might have external props, component state or other mutations influencing the layout tree. In those cases, you may pass a function that maps **layout state** into a tree, with the following signature: `(layoutState) => <ReactElement>`. Here's an example:

```js
const mapLayoutStateToLayoutTree = ({ variant }) => <PrimaryLayout variant={ variant } />;
Expand All @@ -214,7 +214,7 @@ The page component to wrap.

Type: `function`

Allows to dynamically change the layout state. Has the following signature: `(newState | updater?)`.
Allows dynamic changes to the layout state. Has the following signature: `(newState | updater?)`.

The behavior of `setLayoutState` is exactly the same as [`setState`](https://reactjs.org/docs/react-component.html#setstate) of class components: it merges properties and it supports both an object or an updater function.

Expand All @@ -228,8 +228,8 @@ import styles from './about.module.css';

const About = ({ setLayoutState }) => {
const handleSetToDark = useCallback(() => {
setLayoutState({ variant="dark" });
// ..or setLayoutState((layoutState) => ({ variant="dark" }));
setLayoutState({ variant: 'dark' });
// ..or setLayoutState((layoutState) => ({ variant: 'dark' }));
}, [setLayoutState]);

return (
Expand All @@ -242,7 +242,7 @@ const About = ({ setLayoutState }) => {

const mapLayoutStateToLayoutTree = ({ variant }) => <PrimaryLayout variant={ variant } />;

export default withLayout(mapLayoutStateToLayoutTree, { variant: "light" })(About);
export default withLayout(mapLayoutStateToLayoutTree, { variant: 'light' })(About);
```

## Tests
Expand Down
4 changes: 2 additions & 2 deletions src/LayoutTree.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ export default class LayoutTree extends PureComponent {
static getDerivedStateFromProps(props, state) {
const { Component, pageProps } = props;

const didPageChanged = props.Component !== state.Component;
const layoutTree = didPageChanged ? getInitialLayoutTree(Component, pageProps) : state.layoutTree;
const didPageChange = props.Component !== state.Component;
const layoutTree = didPageChange ? getInitialLayoutTree(Component, pageProps) : state.layoutTree;

return {
Component,
Expand Down
2 changes: 1 addition & 1 deletion src/util/full-tree.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const addPageToLayoutTree = (layoutNode, page) => cloneElement(layoutNode, {

const createFullTree = (layoutTree, page) => {
if (!layoutTree) {
return layoutTree;
return;
}

validateLayoutTree(layoutTree);
Expand Down
23 changes: 0 additions & 23 deletions src/util/use-class-state.js

This file was deleted.

23 changes: 23 additions & 0 deletions src/util/use-object-state.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { useState, useRef } from 'react';

const createSetObjectState = (setState) => (updater) => {
updater = typeof updater === 'function' ? updater : () => updater;

return setState((state) => ({
...state,
...updater(state),
}));
};

const useObjectState = (initialState) => {
const [state, setState] = useState(() => initialState ?? {});
const setObjectStateRef = useRef();

if (!setObjectStateRef.current) {
setObjectStateRef.current = createSetObjectState(setState);
}

return [state, setObjectStateRef.current];
};

export default useObjectState;
4 changes: 2 additions & 2 deletions src/with-layout.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React, { useContext, useMemo, forwardRef, useEffect, useRef } from 'react';
import hoistNonReactStatics from 'hoist-non-react-statics';
import useClassState from './util/use-class-state';
import useObjectState from './util/use-object-state';
import LayoutContext from './util/context';

const toFunction = (fn) => typeof fn === 'function' ? fn : () => fn;
Expand All @@ -22,7 +22,7 @@ const withLayout = (mapLayoutStateToLayoutTree, mapPropsToInitialLayoutState) =>
}

const { updateLayoutTree } = useContext(LayoutContext);
const [layoutState, setLayoutState] = useClassState(initialLayoutStateRef.current);
const [layoutState, setLayoutState] = useObjectState(initialLayoutStateRef.current);

useEffect(() => {
if (layoutState !== initialLayoutStateRef.current) {
Expand Down

0 comments on commit 67136e0

Please sign in to comment.