react-Apollo中替代compose的方法是什么?

问题描述 投票:0回答:2

我一直在尝试一次使用多个graphql()增强器,但是撰写似乎没有用。我试过从不同的库中导入不同的负载,但仍然没有。请问有什么解决办法吗?数据没有从graphl()传递到我的组件props,所以我收到一个错误,显示为无法解构'data'的属性'loading',因为它是未定义的。

这是我的组件

import React,{useState} from 'react'

// import {compose} from 'redux'
import { flowRight as compose } from 'lodash'
import { graphql} from 'react-apollo'
import {getAuthorsQuery, addBookMutation} from './queries/query'




const AddBook = (props) => {

  const [formData, setFormData] = useState({
    name:'',
    genre:'',
    authorId:''
  })

  const {name, genre, authorId} = formData
  const onChange = e => setFormData({ ...formData, [e.target.name]: e.target.value})

 const displayAuthors = () => {
   let data = props.data
   const {loading} = data

   if(loading){
   return (<option>Loading Authors</option>) 
   } else{
      return data.authors.map(author => {
       return (<option key={author.id} value={author.id}>{author.name}</option>)
      })
   }
  }

  const submitForm = e => {
   e.preventDefault();
   console.log(formData);

  }

  return (
    <form id="add-book" onSubmit={e => submitForm(e)}>
      <div className="field">
        <label>Book name:</label>
        <input 
        type="text"
        onChange={e => onChange(e)} 
        name="name"
        value={name}
         />
      </div>

      <div className="field">
        <label>Genre:</label>
        <input 
        name="genre"
        value={genre}
        type="text" 
       onChange={e => onChange(e)}
       />
      </div>

      <div className="field">
        <label>Author:</label>
        <select
        name="authorId"
        value={authorId}
        onChange={e => onChange(e)}
        >
        <option>Select Author</option>
        {displayAuthors()}
        </select>
      </div>

      <button>+</button>

    </form>
  )
}

// export default graphql(getAuthorsQuery)(AddBook)

export default compose(
  graphql(getAuthorsQuery, {name: "getAuthorsQuery"}),
  graphql(addBookMutation, {name:"addBookMutation"})
)(AddBook)

和我的查询

   import { gql } from 'apollo-boost'

const getBooksQuery = gql`
{
 books{
     name,
     id
 }
}
`

const getAuthorsQuery = gql`
{
 authors{
     name,
     id
 }
}
`

const addBookMutation = gql`
mutation{
  addBook(name:"", genre:"", authorId:""){
    name,
    id
  }
}
`
export {getAuthorsQuery, getBooksQuery, addBookMutation}
reactjs graphql react-apollo apollo-client
2个回答
0
投票

您可以利用apollo的useQuery挂钩并执行类似的查询

import { useQuery } from '@apollo/react-hooks';

import React,{useState} from 'react'

// import {compose} from 'redux'
import { flowRight as compose } from 'lodash'
import {getAuthorsQuery, addBookMutation} from './queries/query'


const AddBook = (props) => {

  const [formData, setFormData] = useState({
    name:'',
    genre:'',
    authorId:''
  })
  const  authors = useQuery(getAuthorsQuery):
  const bookMutation = useQuery(addBookMutation);

  ... rest of code here


}

export default AddBook;

0
投票

您同时为两个HOC提供了name选项,因此数据将在这些prop名称下可用:

props.getAuthorsQuery.loading

也就是说,HOC已过时-您可能应该使用较新的hooks API。

© www.soinside.com 2019 - 2024. All rights reserved.