我是 react 的新手,正在尝试学习 redux。我想访问课堂内的商店,但它给了我一个错误,我不能在课堂上使用钩子。
当我在函数中使用此代码时(正如我在 YouTube 教程中看到的那样),它可以正常工作。在这里,我可以访问商店的柜台。
function App() {
const counter = useSelector(state => state.counter);
return <div>{counter}</div>;
}
Run Code Online (Sandbox Code Playgroud)
但是当我想在课堂上这样做时,它给了我一个错误,我不能在课堂上使用钩子。
那么如何在类组件中访问我的商店 useSelector 或 useDispatch 呢?
我正在尝试学习如何在 React js 中使用错误边界。正如我在这里读到的那样,它可以防止您的应用程序崩溃。但是我的应用程序崩溃了!这是我的代码:
应用组件:
function App() {
return (
<>
<h1>app component</h1>
<ErrorBoundary >
<Simple />
</ErrorBoundary>
</>
);
}
Run Code Online (Sandbox Code Playgroud)
简单组件:
export default () => {
throw new Error('I crashed!')
return <h3> simple component</h3>
}
Run Code Online (Sandbox Code Playgroud)
这是我的边界=
class ErrorBoundary extends React.Component {
constructor(props) {
super(props);
this.state = { hasError: false };
}
static getDerivedStateFromError(error) {
// Update state so the next render will show the fallback UI.
console.log("I got you ")
return { hasError: true }; …Run Code Online (Sandbox Code Playgroud)