我正在阅读lynda的教程:“构建和部署全栈反应应用程序”,在“注入中继网络层”一章中。在index.js中,尝试设置网络层,程序编译成功,但我在浏览器中收到以下错误:
TypeError:__ WWEPACK_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')
)
答案 0 :(得分:1)
使用反应中继的经典版本可能更容易。
import Relay from 'react-relay/classic'
答案 1 :(得分:0)
发现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