为什么我要获得不确定的道具?
下面是我在 React 中的 Header 组件:
import React from 'react'
import {AppBar} from '@material-ui/core'
import {Typography,Container,Toolbar,Select,MenuItem} from '@material-ui/core'
import {useNavigate} from 'react-router-dom'
import { CryptoState } from './CryptoContext'
const Header = () => {
const navigate=useNavigate();
const {currency,setCurrency}=CryptoState();
return (
<AppBar>
<Container>
<Toolbar>
<Typography onClick={()=>navigate('/')}>
CryptoTracker
</Typography>
<Select value={currency}
onClick={(e)=>setCurrency(e.target.value)}
>
<MenuItem value="USD">USD</MenuItem>
<MenuItem value="INR">INR</MenuItem>
</Select>
</Toolbar>
</Container>
</AppBar>
)
}
export default Header
我使用 ContextAPI 进行高效的状态管理。CryptoState 用于此目的。我已在 Header 中导入状态并使用对象解构获取必要的 props。
下面是我的上下文文件:
import React from 'react'
import { useEffect } from 'react';
import { createContext ,useState,useContext } from 'react';
const CryptoContext = ({children}) => {
const Crypto=createContext();
const [currency,setCurrency]=useState("USD");
const [symbol,setSymbol]=useState("$");
useEffect(()=>{
if(currency=="INR")
setSymbol("₹")
else
setSymbol("$")
})
return (
<Crypto.Provider value={{currency,symbol,setCurrency}}>
{children}
</Crypto.Provider>
)
}
export default CryptoContext;
export const CryptoState=()=>{
return useContext(Crypto);
}
我在标头组件中收到此错误:Header.js:8 Uncaught TypeError: Cannot destruct property 'currency' of '(0 , _CryptoContext__WEBPACK_IMPORTED_MODULE_1__.CryptoState)(...)'未定义。
Below is my Header component in react:
import React from 'react'
import {AppBar} from '@material-ui/core'
import {Typography,Container,Toolbar,Select,MenuItem} from '@material-ui/core'
import {useNavigate} from 'react-router-dom'
import { CryptoState } from './CryptoContext'
const Header = () => {
const navigate=useNavigate();
const {currency,setCurrency}=CryptoState();
return (
<AppBar>
<Container>
<Toolbar>
<Typography onClick={()=>navigate('/')}>
CryptoTracker
</Typography>
<Select value={currency}
onClick={(e)=>setCurrency(e.target.value)}
>
<MenuItem value="USD">USD</MenuItem>
<MenuItem value="INR">INR</MenuItem>
</Select>
</Toolbar>
</Container>
</AppBar>
)
}
export default Header
I have used ContextAPI for efficient state Management.CryptoState is used for this purpose.I have imported the state in Header and getting the necessary props using object-destructuring.
Below is my Context-file:
import React from 'react'
import { useEffect } from 'react';
import { createContext ,useState,useContext } from 'react';
const CryptoContext = ({children}) => {
const Crypto=createContext();
const [currency,setCurrency]=useState("USD");
const [symbol,setSymbol]=useState("quot;);
useEffect(()=>{
if(currency=="INR")
setSymbol("₹")
else
setSymbol("quot;)
})
return (
<Crypto.Provider value={{currency,symbol,setCurrency}}>
{children}
</Crypto.Provider>
)
}
export default CryptoContext;
export const CryptoState=()=>{
return useContext(Crypto);
}
I am getting this error in Header component:Header.js:8 Uncaught TypeError: Cannot destructure property 'currency' of '(0 , _CryptoContext__WEBPACK_IMPORTED_MODULE_1__.CryptoState)(...)' as it is undefined.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在您的
CryptoState
函数中,Crypto
未定义,因为它是在CryptoContext
中初始化的。Crypto
上下文应从您的CryptoContext
组件中提取,并在任何可用函数之外声明为 constinside your
CryptoState
function,Crypto
is not defined as it is initialized inCryptoContext
.Crypto
context should be extracted from yourCryptoContext
component and declared as a const outside of any function to be available