Skip to content
This repository was archived by the owner on Sep 10, 2022. It is now read-only.
This repository was archived by the owner on Sep 10, 2022. It is now read-only.

Rewriting mapPropsStream with Hooks or new Lifecycle Methods #783

@jameslaneconkling

Description

@jameslaneconkling

As a result of React's deprecation of the componentWillMount and componentWillReceiveProps lifecycle hooks, recompose's mapPropsStream now warns

Warning: componentWillMount has been renamed, and is not recommended for use. See https://fb.me/react-async-component-lifecycle-hooks for details.

I've been looking for an equivalent implementation using either Hooks, the new Suspense API, or new lifecycle methods, without much luck. The following works using the unsafe lifecycle methods

const mapPropsStream = (project) => (wrappedComponent) =>
  class MapPropsStream extends Component {
    
    state = { mappedProps: undefined }
    props$ = new Subject()

    componentDidMount() {
      this.subscription = this.props$.pipe(startWith(this.props), project).subscribe((mappedProps) => {
        this.setState({ mappedProps })
      })
    }

    UNSAFE_componentWillReceiveProps(nextProps) {
      this.props$.next(nextProps)
    }

    shouldComponentUpdate(props, state) {
      return this.state.mappedProps !== state.mappedProps
    }

    componentWillUnmount() {
      this.subscription.unsubscribe()
    }

    render() {
      return this.state.mappedProps === undefined ?
        null :
        createElement(wrappedComponent, this.state.mappedProps)
    }
  }

However, I haven't been able to accomplish the same thing without UNSAFE_componentWillReceiveProps. Given that renders are triggered by changes to the props$ props stream, and not the props themselves, I suspect that the Suspense API could be helpful. Curious if anyone else is tackling the same issue.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions