【问题标题】:React warnings using react-hot-loader:使用 react-hot-loader 反应警告:
【发布时间】:2017-08-16 06:25:39
【问题描述】:

我收到如下错误

React.createElement: type is invalid -- 期望一个字符串(对于内置组件)或一个类/函数(对于复合组件)但得到:对象。

还有

未捕获的类型错误:无法读取 null 的属性“unmountComponent”

尝试在我的基本设置中使用react-hot-loader

webpack.config.js

const path = require("path")
const HtmlWebpackPlugin = require("html-webpack-plugin")
const ExtractTextPlugin = require("extract-text-webpack-plugin")

const context = path.resolve(__dirname, "src")

module.exports = {
  context,
  entry: [
    "react-hot-loader/patch",
    "./js/index.js"
  ],
  output: {
    path: path.resolve(__dirname, "build/js"),
    filename: "index.js"
  },
  module: {
    rules: [
      {
        test: /\.(js|jsx)$/,
        loader: "babel-loader",
        options: {
          plugins: [
            [
              "react-css-modules",
              {
                context
              }
            ]
          ]
        }
      },
      {
        test: /\.css$/,
        use: ExtractTextPlugin.extract({
          fallback: "style-loader",
          use: [
            {
              loader: "css-loader",
              options: {
                modules: true,
                importLoaders: 1,
                localIdentName: "[path]___[name]__[local]___[hash:base64:5]",
                sourceMap: true
              }
            },
            'postcss-loader'
          ]
        })
      }
    ]
  },
  plugins: [
    new HtmlWebpackPlugin({
      template: "./index.html",
      filename: "index.html",
      inject: "body"
    }),
    new ExtractTextPlugin("css/app.css")
  ],
  devServer: {
    contentBase: path.resolve(__dirname, "src"),
    historyApiFallback: true
  },
  devtool: "eval"
}

index.js

import ReactDOM from "react-dom"
import React from "react"
import {AppContainer} from "react-hot-loader"
import App from "./App.jsx"
import "../css/global.css"

const render = Component => {
  ReactDOM.render(
    <AppContainer>
      <Component />
    </AppContainer>,
    document.getElementById("app")
  )
}

render(<App />)

if (module.hot) {
  module.hot.accept("./App.jsx", () => render(<App />))
}

App.jsx

即使我只是这样做

export default () => <h1>Hello</h1>

我收到错误

我正在上一个更完整的课程

import React from 'react'
import {createStore, applyMiddleware} from "redux"
import {Provider} from "react-redux"
import createHistory from "history/createBrowserHistory"
import {Route, Switch} from "react-router"
import {NavLink} from "react-router-dom"
import {ConnectedRouter, routerMiddleware} from "react-router-redux"
import {
  Layout,
  Panel,
  AppBar,
} from "react-toolbox"
import Navigation from "react-toolbox/lib/navigation"

import financeApp from "./reducers"
import SbpInvestmentsIndex from "./sbpInvestments/SbpInvestmentsIndex.jsx"
import Http404 from "./Http404.jsx"
import "./app.css"

const history = createHistory()
const historyMiddleware = routerMiddleware(history)
const store = createStore(
  financeApp,
  applyMiddleware(historyMiddleware)
)

export default class App extends React.Component {
  constructor(props) {
    super(props)
    this.state = {
      drawerActive: false
    }

    this.toggleDrawerActive = this.toggleDrawerActive.bind(this)
  }

  toggleDrawerActive() {
    this.setState({
      drawerActive: !this.state.drawerActive
    })
  }

  render() {
    return (
      <Provider store={store}>
        <ConnectedRouter history={history}>
          <Layout>
            <Panel>
              <AppBar title="Personal Finance">
                <Navigation type="horizontal">
                  <NavLink to="/" styleName="app-link">
                    SBP Investments
                  </NavLink>
                </Navigation>
              </AppBar>

              <Switch>
                <Route exact path="/" component={SbpInvestmentsIndex} />
                <Route component={Http404} />
              </Switch>
            </Panel>
          </Layout>
        </ConnectedRouter>
      </Provider>
    )
  }
}

这里有什么问题? Github

上的代码

【问题讨论】:

    标签: javascript reactjs webpack react-hot-loader


    【解决方案1】:

    render() 内部,您正在尝试渲染Component,它在函数调用之前已经被实例化(&lt;... /&gt;)。

    相反,将组件本身传递给render()

    render(App)
    
    if (module.hot) {
      module.hot.accept("./App.jsx", () => render(App))
    }
    

    【讨论】:

      猜你喜欢
      • 2020-08-18
      • 2017-01-09
      • 2016-05-30
      • 2015-12-03
      • 2018-07-22
      • 2018-01-22
      • 2018-11-17
      • 2016-02-28
      • 2017-01-11
      相关资源
      最近更新 更多