-
Notifications
You must be signed in to change notification settings - Fork 27.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
how to transfer values between redux and apollo-client ? #1964
Comments
@jpabbuehl I'd modify withData.js with
Notice that I added store to the parameters. Now in your search.js you can access Redux store to get the data like this.
And to access the any Redux's state in your search.js page
Not sure if this is the correct way but this works for me. |
@tsupol static async getInitialProps (ctx, store) {
let serverState = {}
// Evaluate the composed component's getInitialProps()
let composedInitialProps = {}
if (ComposedComponent.getInitialProps) {
composedInitialProps = await ComposedComponent.getInitialProps({...ctx, store})
}
... @timneutkens: The Apollo examples detailing how to wire Apollo and Redux together are great but it's been difficult to adapt them to work with handling the composed component (which is an awesome implementation) in withData.js |
@theednaffattack here is my quick code (not tested) that might help import React from 'react'
// GQL
import { gql, graphql } from 'react-apollo'
// Redux
import { connect, compose } from 'react-redux'
import { bindActionCreators } from 'redux'
// Redux actions if needed
import { doReduxStuff } from '../../redux/filterActions'
class reactClass extends React.Component {
constructor (props) {
super(props)
}
onClick = (e) => {
e.preventDefault()
// dispatch a Redux action
this.props.action.doReduxStuff({params: 'test'})
}
render () {
// Note: no GQL code here for simplification
return (
<div>
<h1>Data from Redux: {this.props.yourData}</h1>
<button onClick={this.onClick}>Test Redux</button>
</div>
)
}
}
// You may need to pay attention here
const reduxWrapper = connect(
// I think this is what you are looking for
state => ({
yourData: state.yourData
}),
// You can also map dispatch to props
dispatch => ({
actions: {
doReduxStuff: bindActionCreators(doReduxStuff, dispatch),
}
}))
// Apollo!
const query = gql`
query users($id: Int!) {
users(id: $id) {
id
name
}
}
`
const gqlWrapper = graphql(query, {
options: (ownProps) => ({
variables: {
id: ownProps.id,
}
}),
})
// `compose` makes wrapping component much easier and cleaner
export default compose(
reduxWrapper,
gqlWrapper,
)(reactClass) |
@tsupol |
@tsupol |
@tsupol is this only working for react-apollo prior to their latest 2.0.x release or also for the latest one? |
@natterstefan I think it should work also with Apollo 2, but do you really need Redux? |
@tsupol Thanks! |
Hi
looking at the example with-redux-and-apollo, I can't figure out how to copy a store's value to props such that it is passed to a child component performing a graphql query. Here's my current approach. I would greatly appreciate any hint, since using { connect } + MapStateToProps approach from 'react-redux' cannot access the store.
./lib/store.js
./page/search.js
./containers/SearchResult.js
The text was updated successfully, but these errors were encountered: