-
Notifications
You must be signed in to change notification settings - Fork 786
Compose function not working as expected #315
Comments
This is surprising, I would have expected it to work both ways. I wonder if perhaps it's something to do with |
I don't have time right now, have a long flight today, but I will see what I can do in the next couple of days. |
I had the same issue, a very simple component with a search input text and a tabular data filtered by the search input.
I had to put graphql(TestQuery, queryOptions) at the bottom as pointed by @ryannealmes
At this point the client is aware of the changed |
https://github.com/reactjs/redux/blob/master/docs/api/compose.md Compose is just like lodash flowRight. So functions will read right to left or top to bottom however visually you write your HOC. I think we can close this issue |
Why does the order being left to right or right to left matter?
This is obviously not true if what is reported above occurs. In fact I'm having the same issue the previous posters reported - only worse. I can't get react-apollo and react-redux to work together with the same component.
This doesn't work. Nor does putting connect() as the first item in the compose() list. Following mapStateToProps (even an empty one, that just returns {}), the other HOC functions aren't called. Not sure what the issue is, to be honest, but it seems like it is indeed with react-redux and not react-apollo. |
Could one of you provide a reproduction of this with https://github.com/apollographql/react-apollo-error-template please? Thanks! 🙂 |
I think the issue I encountered was having multiple versions of redux or react-redux installed. Non deterministic NPM FTW. |
This issue still exists in react-apollo 1.4.10. The code below fails.
The code works as intended if I make this change:
However, according to the documentation this shouldn't be the case. connect() should be called first (to map state to props) and then be available to graphql(). I will investigate further, but perhaps this should be reopened. |
Did you |
Yes, redux is properly installed and I even checked the content of the compose right before it's used The fact is that even though it does not matter in which order you place functions in compose (mentioned in the documentation), it does matter if one function depends on another. If graphql() has variables that require props from connect, then connect() must be placed above graphql(), so the result of compose() will be connect(graphql(component)). If someone else can confirm this, perhaps it should be added as a note in the documentation. |
Compose is a really simple function, and its implementation (whether from react-apollo, redux or recompose) I doubt is faulty. But you can try all 3. Does any of them work while the others does not? I am nearly certain the issue is multiple co-existing versions of Redux. Can you try:
and tell us what happens? Does it solve the issue? |
At this point I am quite certain that this is not an issue with compose, at least not in my case. |
@deilv Could you please elaborate on the variable scopes. I'm still confused with the order and would believe that |
@andruschenko The thing you need to understand is that both functions inside compose are actually factory functions and return a function which is then called. The outside function can provide context to the inside function. This means that connect() provides state and dispatch methods to graphql(). Note, however, that this only matters if graphql requires connect data for its parameters (store state) to initialize, like in my example. If it does not, then you can call them in any order you want, because all functions within compose will always be available to the original component, which is provided as raw material to the factory. |
@deilv Thanks for your reply! So does it work like this?: Is that somewhat correct? |
@andruschenko not quite.
I'm sorry if this doesn't make it clear enough for you, but this GitHub issue isn't the best place for this discussion. Perhaps you can seek further assistance in stackoverflow or just take my word for it and use the ordering I suggested. :) |
Hey compose experts, anyone know how to annotate a compose block with flow type? I found a simple example here and it works but it's not nearly as concise as We do happen to import Much appreciated! |
@TSMMark I don't see any examples using flow + compose. Any chance you have a link you could point me to? |
@SpencerCDixon the best I could do was
type OwnProps = {
foo: string
}
type StateProps = {
somethingFromRedux: string
}
type Props =
& OwnProps
& StateProps
const withQuery: OperationComponent<myQuery, OptionDescription<StateProps>, Props> = graphql(query, queryOptions)
const connected: React$ComponentType<OwnProps> = compose(
connect(mapStateToProps, mapDispatchToProps),
withQuery,
injectSheet(styles),
withWhateverElse,
)(MyComponent) Not sure if there's a better way but this does work. |
ahhh, I see. Thank you so much for the snippet!! 😄 |
Hi, I keep getting a similar issue: "undefined is not a function (near '...(0, _reactApollo.compose)...')" The issue keeps telling me to look at:
has anyone encounter similar issue? I know for a fact the issue is on compose but no idea how to fix |
@Madgeniusblink The compose utility was recently removed from react-apollo (check the changelog for v3.0.0 |
Using the compose function I ran into a problem where props were not being passed to my connected component state changes.
I was using the compose function below
after changing to
I was using the compose function below
I think some things required to reproduce might be related to my setup.
"apollo-client": "0.5.0",
"react-apollo": "0.5.16",
"graphql-tag": "0.1.15",
This might not be a bug, but I thought I would mention it since the documentation connects the redux store after the graphql query and mutations. If this intended, it may be worth mentioning in the docs that order matters.
http://dev.apollodata.com/react/higher-order-components.html#
The text was updated successfully, but these errors were encountered: