简体   繁体   English

反应采用突变两次

[英]Mutation fires twice with react-adopt

I am using react-adopt with react-apollo . 我正在使用react-adopt with react-apollo The simple reason for this is that having a large number of queries and mutations on a single page gets very messy. 原因很简单,在单个页面上进行大量查询和突变会变得非常混乱。

Issue 问题

  • When firing mut1 only one mutation will happen. mut1只会发生一个突变。
  • When firing mut2 , mut2 fires and then mut1 fires once mut2 finishes. 当激活mut2mut2会激活,然后mut1会在mut2完成后触发。
  • If I had a mu3,4,5 etc and I clicked mut3 it would run mut3 > mut2 and 如果我有一个mu3,4,5 etc我点击mut3它会运行mut3 > mut2
  • the finally mut1 If you ran mut5 it would then do 4 > 3 > 2 > 1 etc... 最后mut1如果你运行mut5它会做4 > 3 > 2 > 1等...

Component 零件

const mut1 = () => {
  return (
    <Mutation mutation={MUTATION} {...props} />
  )
}
const mut2 = () => {
  return <Mutation mutation={MUTATION2} {...props} />
}
export default
  adopt({
    mut1,
    mut2,
  }
)

I would of course like each mutation to only fire once when clicked. 我当然希望每个突变只在点击时触发一次。

create MutationContainer.js which contain all your mutation export MutationContainer as a variable 创建MutationContainer.js ,其中包含您的所有突变导出MutationContainer作为变量

import { gql } from 'apollo-boost'
import { adopt } from 'react-adopt'

const mutation1 = () => (
  <Mutation mutation={MUTATION1} {...props} />
)
const mutation2 = () => (
  <Mutation mutation={MUTATION2} {...props} />
)
const mutation3 = () => (
  <Mutation mutation={MUTATION3} {...props} />
)
export const MutationContainer = adopt({
  mutation1,
  mutation2,
  mutation3
})

Import it on any component where you want to use this mutations 将其导入您要使用此突变的任何组件

on your Component1.jsx 在您的Component1.jsx上

import { adopt } from 'react-adopt'
import { Value } from 'react-powerplug'

import { MutationContainer } from './MutationContainer'

const Composed = adopt({
  input: <Value initial="" />,
  container: <MutationContainer />,
})

export const Component1 = () => (
  <Composed>
    {({ container: { mutation1 }, input }) => {
      const handleAdd = ev => {
        mutation1.mutation({ variables: { ...variables } })
        input.setValue('')
        ev.preventDefault()
      }
      return (
        <Form onSubmit={handleAdd}>
          <Input
            type="text"
            value={input.value}
            onChange={ev => input.setValue(ev.target.value)}
          />
          <Button type="submit">Let's call mutation1</Button>
        </Form>
      )
    }}
  </Composed>
)

On Component2.jsx 在Component2.jsx上

import { adopt } from 'react-adopt'
import { Value } from 'react-powerplug'
import { MutationContainer } from './MutationContainer'

const Composed = adopt({
  input: <Value initial="" />,
  container: <MutationContainer />,
})

export const Component2 = () => (
  <Composed>
    {({ container: { mutation2, mutation3 }, loading }) => {

      const handleMutation2 = async () => {
        await mutation2.mutation({ variables: { ...variables } })
      }
      const handleMutation3 = async () => {
        await mutation3.mutation({ variables: { ...variables } })
      }
      return (
        <React.Fragment>
          <Button onClick={handleMutation2}>
           Let's call mutation2
          </Button>
          <Button onClick={handleMutation3}>
            Let's call mutation3
          </Button>
        <React.Fragment>
      )
    }}
  </Composed>
)

Please take care of variables as par your requirement 请根据您的要求处理变量

And most important point based on your condition 最重要的一点是根据你的情况

export const App = () => (
  <MutationContainer>
    {({ data: { loading, data } }) => (
        <React.Fragment>
         <Component1  {...this.props }/>
         <Component2 {...this.props } />
        </React.Fragment>
    )}
  </MutationContainer>
)

If you persist this problem please let me know. 如果您坚持这个问题,请告诉我。 will try react-adopt with new approach. 将尝试react-adopt新方法react-adopt

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM