Recompose: Getting access to 'this'

Created on 4 Nov 2016  路  3Comments  路  Source: acdlite/recompose

Is there a pattern or way to get access to the wrapped component's this from within any of the composed methods?

For example, I would like to be able to do this:

withHandlers({
    onError: props => (e) => {
      this.setState({error: e})
    }
  })

Most helpful comment

withState('state', 'setState', null),
withHandlers({
    onSuccess: ({ setState }) => () => {
      setState({ requests, loading: false, error: undefined });
    },
    onError: ({ setState }) => () => {
      setState({ error: e, loading: false });
    }
})

Always using props is much nicer, you are decoupling yourself from any particular api, since you are just calling a passed in prop.

All 3 comments

if you want to do that, you may need to use withState(), so the code will be sth like this

javascript
withState('error', 'setError', null),
withHandlers({
onError: ({ setError }) => (e) => {
setError(e);
}
})
```

Let me know if it helps you 馃槈

But this puts them on the props object right? It seems like this library kind of forces you to use props for everything, but maybe I'm not using it correctly.
Do I have to change from a pattern like this:

    Data.Query.run(q).then(results => results.requests)
      .then(requests => this.setState({ requests, loading: false, error: undefined }))
      .catch(e => this.setState({ error: e, loading: false }));

to

    Data.Query.run(q).then(results => results.requests)
      .then(requests => {
        this.setState({ requests }); 
        this.props.setError(undefined); 
        this.props.setLoading(false);
      }
      .catch(e => 
        .then(requests => {
        this.props.setError(e); 
        this.props.setLoading(false);
      }
     );
withState('state', 'setState', null),
withHandlers({
    onSuccess: ({ setState }) => () => {
      setState({ requests, loading: false, error: undefined });
    },
    onError: ({ setState }) => () => {
      setState({ error: e, loading: false });
    }
})

Always using props is much nicer, you are decoupling yourself from any particular api, since you are just calling a passed in prop.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

istarkov picture istarkov  路  3Comments

jethrolarson picture jethrolarson  路  4Comments

franklinkim picture franklinkim  路  3Comments

jeron-diovis picture jeron-diovis  路  4Comments

adrianmcli picture adrianmcli  路  3Comments