撰写未从 react-apollo 导出

rol*_*eon 11 apollo reactjs graphql react-apollo

我正在关注 youtube 上的 graphql 教程(https://www.youtube.com/watch?v=ed8SzALpx1Q at about 3hr 16min),其中部分内容compose来自“react-apollo”。但是,我收到一个错误,因为新版本的 react-apollo 没有导出这个。

我在网上看了,我需要更换import { compose } from "react-apollo"import { compose } from "recompose",但这样做会产生错误TypeError: Cannot read property 'loading' of undefined我也看到了,我应该取代反应,从阿波罗与进口import * as compose from "lodash",但是当我这样做,我得到其他错误,他说,× TypeError: lodash__WEBPACK_IMPORTED_MODULE_2__(...) is not a function

应用程序.js:

import React from "react";
import ApolloClient from "apollo-boost";
import { ApolloProvider } from "react-apollo";

import BookList from "./components/BookList";
import AddBook from "./components/AddBook";

//apollo client setup
const client = new ApolloClient({
  uri: "http://localhost:4000/graphql"
});

function App() {
  return (
    <ApolloProvider client={client}>
      <div className="main">
        <h1>My Reading List</h1>
        <BookList />
        <AddBook />
      </div>
    </ApolloProvider>
  );
}

export default App;
Run Code Online (Sandbox Code Playgroud)

查询.js:

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 };
Run Code Online (Sandbox Code Playgroud)

添加Books.js:

import React, { Component } from "react";
import { graphql } from "react-apollo";
import { compose } from "recompose";
// import * as compose from "lodash";
import { getAuthorsQuery, addBookMutation } from "../queries/queries";

class AddBook extends Component {
  state = {
    name: "",
    genre: "",
    authorId: ""
  };

  displayAuthors = () => {
    let data = this.props.data;
    if (data.loading) {
      return <option>loading authors...</option>;
    } else {
      return data.authors.map(author => {
        return (
          <option key={author.id} value={author.id}>
            {author.name}
          </option>
        );
      });
    }
  };

  submitForm(e) {
    e.preventDefault();
    console.log(this.state);
  }

  render() {
    return (
      <form onSubmit={this.submitForm.bind(this)}>
        <div className="field">
          <label>Book name: </label>
          <input
            type="text"
            onChange={e => {
              this.setState({ name: e.target.value });
            }}
          />
        </div>
        <div className="field">
          <label>Genre: </label>
          <input
            type="text"
            onChange={e => {
              this.setState({ genre: e.target.value });
            }}
          />
        </div>
        <div className="field">
          <label>Author: </label>
          <select
            onChange={e => {
              this.setState({ authorId: e.target.value });
            }}
          >
            <option>Select author</option>
            {this.displayAuthors()}
          </select>
        </div>
        <button>+</button>
      </form>
    );
  }
}

export default compose(
  graphql(getAuthorsQuery, { name: "getAuthorsQuery" }),
  graphql(addBookMutation, { name: "addBookMutation" })
)(AddBook);
Run Code Online (Sandbox Code Playgroud)

我希望从 react-apollo 导入 compose 并获取查询和变异并使它们在 AddBook 的 props 内可用,所以我可以在 displayAuthors() 和 submitForm() 函数中使用它们,但相反我得到了它的错误不是从 react-apollo 导出的,当我尝试在网上找到的建议解决方案时,我遇到了上面提到的其他错误。

Sté*_*ber 17

compose已从 React Apollo 3.0.0 中删除。如果您想使用相同的 HOC 模式,请随意使用 lodash 的flowRight.

在您的客户端文件夹中安装 lodash

npm install lodash 
Run Code Online (Sandbox Code Playgroud)

并使用它从 lodash 导入 compose(在 flowRight 中使用大写 R)

import {flowRight as compose} from 'lodash';
Run Code Online (Sandbox Code Playgroud)

刹车变化参考

  • 在开始使用黑客解决方案之前,我想知道为什么“composer”被删除。如果 compose 被删除是因为这是一种不好的做法或其他原因,那么更好的方法是什么。 (2认同)

Ala*_*lan 6

compose已从 React Apollo 3 中删除(请参阅 Breaking Changes)。现在,要使用 compose,请使用 lodash 的 flowRight。

安装流程权:

yarn add lodash.flowright

在您的代码中替换:

import { compose } from 'react-apollo'

经过

import {flowRight as compose} from 'lodash';