React relay injectNetworkLayer is not a function

2019-07-20 15:20发布

I am following the tutorial from lynda: "Building and Deploying a Full-Stack React Application", in the chapter "Injecting the Relay Network Layer". there is in index.js, an attempt to set up a network layer, and the program compiles successfully but I'm receiving the following error in the browser:

TypeError: __WEBPACK_IMPORTED_MODULE_4_react_relay___default.a.injectNetworkLayer is not a function

index.js file is:

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')
)

2条回答
霸刀☆藐视天下
2楼-- · 2019-07-20 15:27

Duplicate question.

Found Chris Mazzouchi's answer helpful, though I had to uninstall and re-install the following specific versions to get it working:

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
查看更多
对你真心纯属浪费
3楼-- · 2019-07-20 15:40

It might be easier to just use the classic version of react-relay. import Relay from 'react-relay/classic'

查看更多
登录 后发表回答