React 中继 injectNetworkLayer 不是函数

React relay injectNetworkLayer is not a function

我正在学习 lynda 的教程:"Building and Deploying a Full-Stack React Application",在 "Injecting the Relay Network Layer" 一章中。在 index.js 中,尝试设置网络层,程序编译成功,但我在浏览器中收到以下错误:

TypeError: __WEBPACK_IMPORTED_MODULE_4_react_relay___default.a.injectNetworkLayer 不是函数

index.js 文件是:

import React from 'react'
import ReactDOM from 'react-dom'
import {Router, browserHistory, applyRouterMiddleware} from 'react-router'
import Routes from './routes'
import Relay from 'react-relay'
import useRelay from 'react-router-relay'
import {RelayNetworkLayer, urlMiddleware} from 'react-relay-network-layer'
import {relayApi} from './config/endpoints'
import auth from './utils/auth'

const createHeaders = () => {
  let idToken = auth.getToken()
  if (idToken) {
    return {
      'Authorization': `Bearer ${idToken}`
    }
  } else {
    return {}
  }
}

Relay.injectNetworkLayer(
  new RelayNetworkLayer([
    urlMiddleware({url: (req) => relayApi,}),
        next => req => {
          req.headers = {
            ...req.headers,
            ...createHeaders()
          }
          return next(req)
      },
  ],{disableBatchQuery: true})
)

ReactDOM.render(
  <Router
    environment={Relay.Store}
    render={applyRouterMiddleware(useRelay)}
    history={browserHistory}
    routes={Routes}
  />,
  document.getElementById('root')
)

发现 Chris Mazzouchi 的回答很有帮助,尽管我必须卸载并重新安装以下特定版本才能使其正常工作:

yarn remove react-relay react-relay-network-layer react-router-relay
yarn add react-relay@0.10.0 react-relay-network-layer@1.3.9 react-router-relay@0.13.5

直接使用 react-relay 的经典版本可能更容易。 import Relay from 'react-relay/classic'