Skip to content

Commit

Permalink
feat: change default example to use the lifecycle helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
mohcinenazrhan committed Feb 16, 2020
1 parent 8dcebb9 commit ea5bf63
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 12 deletions.
79 changes: 67 additions & 12 deletions example/src/App.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,68 @@
import React, { Component } from 'react'

import ExampleComponent from 'react-use-lifecycle-methods'

export default class App extends Component {
render () {
return (
<div>
<ExampleComponent text='Modern React component module' />
</div>
)
}
import React, { useState } from 'react'
import useLifecycleMethods from 'react-use-lifecycle-helpers'

import ChildComponent from "./ChildComponent";

const App = (props) => {
const [state, setState] = useState({ count: 0, bool: false });

const {
useDidMount,
useDepsDidChange,
useDidUpdate,

} = useLifecycleMethods(state, props);

useDidMount(() => {
console.log("useDidMount");
});

useDidUpdate((prevState, prevProps) => {
console.log("useDidUpdate", prevState, state, prevProps, props);
});

useDepsDidChange(
prevState => {
console.log("useDepsDidChange Count", state.count, prevState.count);
},
["count"]
);

useDepsDidChange(
prevState => {
console.log("useDepsDidChange Bool", state.bool, prevState.bool);
},
["bool"]
);

useDepsDidChange(
prevState => {
console.log("useDepsDidChange Count", state.count, prevState.count);
console.log("useDepsDidChange Bool", state.bool, prevState.bool);
},
["bool", "count"]
);

return (
<div>
<button
onClick={() => {
setState({ ...state, count: state.count + 1 });
}}
>
Increment
</button>
<p>{state.count}</p>
<button
onClick={() => {
setState({ ...state, bool: !state.bool });
}}
>
Toggle ChildComponent {state.bool.toString()}
</button>
{state.bool && <ChildComponent />}
</div>
);
}

export default App;
14 changes: 14 additions & 0 deletions example/src/ChildComponent.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import React from "react";
import useLifecycleMethods from 'react-use-lifecycle-helpers'

const ChildComponent = () => {
const { useWillUnmount } = useLifecycleMethods();

useWillUnmount(() => {
console.log("useWillUnmount: ChildComponent");
});

return <div>ChildComponent Content</div>;
};

export default ChildComponent;

0 comments on commit ea5bf63

Please sign in to comment.