acdlite / recompose

A React utility belt for function components and higher-order components.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Impure lifecycle HOC

suzdalnitski opened this issue · comments

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.

see https://github.com/deepsweet/hocs/blob/master/readme.md here lifecycle will possibly be deprecated at all