简体   繁体   English

使用react-hot-loader反应警告:

[英]React warnings using react-hot-loader:

I am getting errors like below 我收到如下错误

React.createElement: type is invalid -- expected a string (for built-in components) or a class/function (for composite components) but got: object. React.createElement:类型无效-预期为字符串(对于内置组件)或类/函数(对于复合组件),但得到了:对象。

And

Uncaught TypeError: Cannot read property 'unmountComponent' of null 未捕获的TypeError:无法读取null的属性'unmountComponent'

Trying to use react-hot-loader with my basic setup. 尝试在我的基本设置中使用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

Even when I just do 即使我刚做

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

I get error 我得到错误

I am doing a more complete class tho 我正在上更完整的课

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

Whats wrong here? 怎么了 Code on Github Github上的代码

Inside render() , you are trying to render Component , which has already been instantiated ( <... /> ) before the function call. render()内部,您尝试渲染Component ,该Component在函数调用之前已经实例化( <... /> )。

Instead, pass the Component itself into render() : 相反,将Component本身传递给render()

render(App)

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM