【发布时间】:2018-04-22 12:40:04
【问题描述】:
我在这里有一个 react 无状态函数,我很想从我的 reducer 中调用我的突变。请注意,在 React.Component 类中调用突变是通过在组件中添加一个函数来实现的,我不希望这样,我想使用我的 reducer 来做到这一点。
通过最少的代码示例,我将展示我的设置
// index.js render function
[...] // imported variables
<ApolloProvider client={client}>
<Provider store={store}>
<ConnectedRouter history={history}>
<div>
<App />
</div>
</ConnectedRouter>
</Provider>
</ApolloProvider>
// Mutation
const Foo = gql`
mutation Foo($id: String) {
AddBar(id: $id) {
id
}
}
`;
// Component
[...] // other imports for below variables
const Comp = (props) => (
<div>
<button onClick={() => props.addFoo(props)}>Click</button>
</div>
)
const mapDispatchToProps = dispatch => bindActionCreators({
addFoo
}, dispatch)
export default connect(
mapDispatchToProps
)(graphql(Foo)(Comp))
// Reducer (very slimmed with just the action)
export const addFoo = (props) => {
// props contained the mutate function
props.mutate({
variables: {id: "1" }
})
// dispatch goes here but not needed.
}
好的,我已经尽可能地精简了这个例子。我的问题是我的变量没有传递给我的变异函数。如果我用一个硬编码id 并单击按钮,graphql 会更改我的数据(是的),但唯一的问题是变量没有传递。在检查器中,我确实看到了具有正确值的变量,但......没有传递给 mutate 函数。
【问题讨论】:
标签: reactjs redux graphql create-react-app