gpt4 book ai didi

reactjs - Uncaught Error : Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: object

转载 作者:行者123 更新时间:2023-12-03 13:43:54 29 4
gpt4 key购买 nike

Reactjs 上下文提供程序错误

我收到以下错误

invariant.js:42 Uncaught Error: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: object.

这是我的app.js

import React from 'react';
import ReactDOM from 'react-dom';
import { UserProvider } from "./lib/user.js"

const App = () => <UserProvider></UserProvider>

ReactDOM.render(
<App />,
document.getElementById("app")
);

这是./lib/user.js:

import React from 'react'

export const UserContext = React.createContext({
user: null,
logIn: ((token, user) => {}),
logOut: (() => {})
})

export class UserProvider extends React.Component {
constructor(props) {
super(props)

this.logIn = (token, user) => {
setToken(token)
this.setState(state => ({user: user}))
}

this.logOut = (client) => {
clearToken()
this.setState(state => ({user: null}))
client.resetStore()
client.cache.reset()
}

this.state = {
user: null,
logIn: this.logIn,
logOut: this.logOut
}
}

render() {
console.log(<div></div>)
console.log(<UserContext.Provider/>)
return (
<UserContext.Provider value={this.state}>
<p>hi</p>
</UserContext.Provider>
)
}
}

这是在 React 16.3.2 上。在很多情况下,错误是由于默认导出与命名导出引起的,但这似乎不是:

  1. Uncaught Error: Invariant Violation: Element type is invalid: expected a string (for built-in components) or a class/function but got: object
  2. Uncaught Error: Element type is invalid: expected a string (for built-in components)
  3. Element type is invalid: expected a string (for built-in components) or a class/function

最佳答案

您可能正在将 react@16.3 与尚不支持上下文的早期 react-dom 版本(例如 16.2)一起使用。

关于reactjs - Uncaught Error : Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: object,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49969225/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com