反应,无法在上下文中消耗价值,获得undurefther typeError:无法读取未定义的属性
我的主页什么都没有渲染。我已经检查了home.js
,我找不到问题。在控制台中,这是我遇到的错误:
Home.js:13 Uncaught TypeError: Cannot read properties of undefined (reading 'name')
app.js:
import React, { useState } from 'react'
import Navigation from './Navigation'
import ThemeContext from './ThemeContext'
import UserContext from './UserContext'
import Home from './Home'
function App() {
let [theme, setTheme] = useState({
variant: 'dark',
toggleTheme: toggleTheme
})
let [user, setUser] = useState({
name: "Alyssa"
})
function toggleTheme() {
setTheme(theme => (
{
...theme,
variant: theme.variant === 'dark' ? 'light' : 'dark',
}
))
}
return (
<>
<ThemeContext.Provider value={theme}>
<Navigation />
</ThemeContext.Provider>
<ThemeContext.Provider value={theme}>
<Home />
</ThemeContext.Provider>
</>
)
}
export default App
home.js:
import Card from 'react-bootstrap/Card'
import Button from 'react-bootstrap/Button'
import Alert from 'react-bootstrap/Alert'
import UserContext from './UserContext'
import { useContext } from 'react'
function Home() {
let user = useContext(UserContext)
return (
<>
<UserContext.Consumer>
<Alert variant="success">Welcome back, {user.name}!</Alert>
<Card className="text-center col-md-10 mx-auto my-3">
<Card.Header>Featured</Card.Header>
<Card.Body>
<Card.Title>This is Our Featured Item</Card.Title>
<Card.Text>
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
</Card.Text>
<Button variant="primary">Click Here</Button>
</Card.Body>
</Card>
</UserContext.Consumer>
</>
)
}
export default Home
My Home page isn't rendering anything. I've checked Home.js
and I can't find what could be the issue. In the console this is the error I get:
Home.js:13 Uncaught TypeError: Cannot read properties of undefined (reading 'name')
App.js:
import React, { useState } from 'react'
import Navigation from './Navigation'
import ThemeContext from './ThemeContext'
import UserContext from './UserContext'
import Home from './Home'
function App() {
let [theme, setTheme] = useState({
variant: 'dark',
toggleTheme: toggleTheme
})
let [user, setUser] = useState({
name: "Alyssa"
})
function toggleTheme() {
setTheme(theme => (
{
...theme,
variant: theme.variant === 'dark' ? 'light' : 'dark',
}
))
}
return (
<>
<ThemeContext.Provider value={theme}>
<Navigation />
</ThemeContext.Provider>
<ThemeContext.Provider value={theme}>
<Home />
</ThemeContext.Provider>
</>
)
}
export default App
Home.js:
import Card from 'react-bootstrap/Card'
import Button from 'react-bootstrap/Button'
import Alert from 'react-bootstrap/Alert'
import UserContext from './UserContext'
import { useContext } from 'react'
function Home() {
let user = useContext(UserContext)
return (
<>
<UserContext.Consumer>
<Alert variant="success">Welcome back, {user.name}!</Alert>
<Card className="text-center col-md-10 mx-auto my-3">
<Card.Header>Featured</Card.Header>
<Card.Body>
<Card.Title>This is Our Featured Item</Card.Title>
<Card.Text>
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
</Card.Text>
<Button variant="primary">Click Here</Button>
</Card.Body>
</Card>
</UserContext.Consumer>
</>
)
}
export default Home
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您没有通过用户被儿童组件消费的用户。您需要使用USERCONTEXT.ROVIDER包装主页,并通过您想要提供的用户。另外,您可以在主页上删除usercontext.consumer标签,因为您正在使用挂钩方法从上下文中消费。
家
You are not passing the user to be consumed by a child component. You need to wrap the Home page with the UserContext.Provider and pass the user that you'd like to provide. Also, you can remove the UserContext.Consumer tag in the Home page since you are using the Hooks approach for consuming from a context.
Home
您需要传递,然后可以使用home.js文件中的props.name访问它。
You need to pass in and then you can access it using props.name in Home.js file also pass props in Home.js function.
这是因为此行
让用户= USECONTEXT(USERERCONTEXT)
正在存储undefined
inuser
use ,这是正常的,因为您没有包装home
内部usercontext.provider
,并给出用户
内部app
作为值。更改
App
内的返回:您可以简化
Home
内的返回:It's because this line
let user = useContext(UserContext)
is storingundefined
insideuser
, which is normal because you didn't wrapHome
insideUserContext.Provider
and gaveuser
insideApp
as value.Change the return inside
App
to this:And you could simplify your return inside
Home
to this:您没有将应用程序组件与UserContext.provider相关联,因此用户对象没有值。尝试此操作
并使用可选的Chaning或操作员访问用户对象,以防止用户无效或未定义
或
You didnt wrap app component with UserContext.Provider so there is no value for user object. Try this
And use optional chaning or and operator to access user object to prevent errors when user is null or undefined
Or