Impure lifecycle HOC
suzdalnitski opened this issue · comments
Ilya Suzdalnitskiy commented
The way lifecycle
is currently implemented is that it requires the use of this
within the lifecycle methods. This goes against the principles of functional programming and introduces impurity into the HOCs.
I propose to introduce alternative lifecycle HOCs that are pure and provide the props/setState as arguments.
Before:
const withFetch = lifecycle({
componentDidMount() {
fetchPosts(this.props.username).then(posts => {
this.setState({ posts });
})
}
});
After:
const withFetch = withComponentDidMount(
({username}, setState) => {
const posts = await fetchPosts(username);
setState({ posts });
}
);
Is this something that would be welcomed in a Pull Request? Thanks.
Ivan Starkov commented
see https://github.com/deepsweet/hocs/blob/master/readme.md here lifecycle will possibly be deprecated at all