-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Rewriting mapPropsStream with Hooks or new Lifecycle Methods #783
Description
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.