[英]Can I use useState hook in class component?
I'm trying to solve a React problem on testdome.我正在尝试解决 testdome 上的 React 问题。
This is the task:这是任务:
The Message component contains an anchor element and a paragraph below the anchor.
Message 组件包含一个锚元素和锚下方的一个段落。 Rendering of the paragraph should be toggled by clicking on the anchor element using the following logic:
段落的渲染应该通过使用以下逻辑单击锚元素来切换:
At the start, the paragraph should not be rendered.
在开始时,不应呈现该段落。
After a click, the paragraph should be rendered.单击后,应呈现该段落。
After another click, the paragraph should not be rendered.再次单击后,不应呈现该段落。
Finish the Message component by implementing this logic.通过实现这个逻辑来完成 Message 组件。
This is the solution:这是解决方案:
// React is loaded and is available as React and ReactDOM
// imports should NOT be used
class Message extends React.Component {
constructor(props) {
super(props);
this.state = {
visible: false
};
}
clickHandler = (event) => {
event.preventDefault();
this.setState({
visible: !this.state.visible
});
}
render() {
return (<React.Fragment>
<a onClick={this.clickHandler} href="#">Want to buy a new car?</a>
{ this.state.visible && <p>Call +11 22 33 44 now!</p>}
</React.Fragment>);
}
}
document.body.innerHTML = "<div id='root'> </div>";
const rootElement = document.getElementById("root");
ReactDOM.render(<Message />, rootElement);
console.log("Before click: " + rootElement.innerHTML);
document.querySelector("a").click();
console.log("After click: " + rootElement.innerHTML);
I want to do the same with useState Hook.我想对 useState Hook 做同样的事情。 Here is my code:
这是我的代码:
// React is loaded and is available as React and ReactDOM
// imports should NOT be used
class Message extends React.Component {
const [show, setShow] = useState(false);
const toggleVisibility = () => {
setShow(!show);
}
render() {
if(show) {
return (<React.Fragment>
<a onClick={toggleVisibility} href="#">Want to buy a new car?</a>
<p>Call +11 22 33 44 now!</p>
</React.Fragment>);
} else {
return (<React.Fragment>
<a onClick={toggleVisibility} href="#">Want to buy a new car?</a>
</React.Fragment>);
}
}
}
document.body.innerHTML = "<div id='root'> </div>";
const rootElement = document.getElementById("root");
ReactDOM.render(<Message />, rootElement);
console.log("Before click: " + rootElement.innerHTML);
document.querySelector("a").click();
console.log("After click: " + rootElement.innerHTML);
This is the Output:这是 Output:
Run OK, but 3 out of 3 test cases failed.
Line 5: SyntaxError: unknown: Unexpected token (5:8)
3 | class Message extends React.Component {
4 |
> 5 | const [show, setShow] = useState(false);
| ^
6 |
7 | const toggleVisibility = () => {
8 | setShow(!show);
Your score is 0% :(
Why does it not work?为什么它不起作用? is it not allowed to use hooks in class components?
是否不允许在 class 组件中使用挂钩? if thats not possible, how can I change the class component into a function component?
如果那不可能,我如何将 class 组件更改为 function 组件?
You cannot use hooks in class components, only for functional components.您不能在 class 组件中使用挂钩,只能用于功能组件。 You could refactor your Message component into a functional component like the following:
您可以将 Message 组件重构为功能组件,如下所示:
export const Message = () => {
const [show, setShow] = useState(false);
const toggleVisibility = () => {
setShow(!show);
}
if(show) {
return (
<React.Fragment>
<a onClick={toggleVisibility} href="#">Want to buy a new car?</a>
<p>Call +11 22 33 44 now!</p>
</React.Fragment>
);
} else {
return (
<React.Fragment>
<a onClick={toggleVisibility} href="#">Want to buy a new car?</a>
</React.Fragment>
);
}
}
You cannot use useState()
hook in a Class based component.您不能在基于 Class 的组件中使用
useState()
挂钩。 To do so switch to a functional based component.为此,请切换到基于功能的组件。
You can create a functional component as below 👇您可以创建如下功能组件👇
import React, { useState } from "react";
import ReactDOM from "react-dom";
const Message = () => {
const [show, setShow] = useState(false);
const toggleVisibility = () => {
setShow(!show);
};
return (
<React.Fragment>
<a onClick={toggleVisibility} href="#">
Want to buy a new car?
</a>
{show && <p>Call +11 22 33 44 now!</p>}
</React.Fragment>
);
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.