网站首页 > 基础教程 正文
通过示例了解State Hook的简单用法以及useState()函数的应用
import { useState } from "react";
const infoMap = {
beginInfo : '您已经单击了',
endInfo : '次按钮。',
btnInfo: '计数器'
}
function HookExampleOne() {
const [count, setCount] = useState(0);
return (
<div>
<p>{infoMap.beginInfo}{count}{infoMap.endInfo}</p>
<button onClick={() => setCount(count+1)}>
{infoMap.btnInfo}
</button>
</div>
);
}
export default HookExampleOne;
import React from "react";
const infoMap = {
beginInfo : '您已经单击了',
endInfo : '次按钮。',
btnInfo: '计数器'
}
export default class HookExampleTwo extends React.Component {
constructor(props){
super(props);
this.state = {
count: 0
};
}
render(){
return (
<div>
<p>{infoMap.beginInfo}{this.state.count}{infoMap.endInfo}</p>
<button onClick={() => this.setState({count: this.state.count + 1})}>{infoMap.btnInfo}
</button>
</div>
)
}
}
import { useState } from "react";
const infoMap = {
beginInfo: '您已经单击了',
endInfo: '次按钮。',
btnInfo: '计数器',
beforeInfo: '猴子已经吃了',
afterInfo: '个桃子。',
ageInfo: '年龄',
fruitInfo: '水果',
doInfo: '待办事项:',
}
export default function HookExampleThree() {
// 声明一个新的state变量count
const [count, setCount] = useState(0);
// 声明多个state变量
const [peach, setPeach] = useState(0);
const [age, setAge] = useState(18);
const [fruit, setFruit] = useState('peach');
const [todos, setTodos] = useState([
{ text: '学习React Hook' }
]);
function handleOrangeClick() {
if (fruit === 'peack'){
setFruit('orange')
} else {
setFruit('peach')
}
}
function handleDoClick() {
alert('学习React Hook')
}
return (
<div>
<div>{infoMap.beforeInfo}{count}{infoMap.endInfo}</div>
<button onClick={() => setCount(count + 1)}>{infoMap.btnInfo}</button>
<div>{infoMap.beginInfo}{peach}{infoMap.afterInfo}</div>
<button onClick={() => setPeach(peach + 1)}>{infoMap.btnInfo}</button>
<div>{infoMap.ageInfo}{age}</div>
<button onClick={() => setAge(age + 1)}>{infoMap.btnInfo}</button>
<div>{infoMap.fruitInfo}{fruit}</div>
<button onClick={handleOrangeClick}>{infoMap.fruitInfo}</button>
<p>{infoMap.doInfo}</p>
<button onClick={handleDoClick}>{infoMap.doInfo}</button>
</div>
);
}
import { useState } from "react";
const infoMap = {
beginInfo: '您已经单击了',
endInfo: '次按钮。',
btnInfo: '计数器',
beforeInfo: '猴子已经吃了',
afterInfo: '个桃子。',
ageInfo: '年龄',
fruitInfo: '水果',
doInfo: '待办事项:',
}
export default function HookExampleFour() {
// 返回一个包含两个元素的数组
let fruitStateVariable = useState('peach');
// 数组里的第一个值
let fruit = fruitStateVariable[0];
// 数组里的第二个值
let setFruit = fruitStateVariable[1];
function handlePeachClick(){
if (fruit === 'peach'){
setFruit('orange')
} else {
setFruit('peach')
}
}
return (
<div>
<p>{infoMap.fruitInfo}{fruit}</p>
<button onClick={handlePeachClick}>{infoMap.fruitInfo}</button>
</div>
)
}
import React from "react";
import HookExampleOne from "./HookExampleOne";
import HookExampleTwo from "./HookExampleTwo";
import HookExampleThree from "./HookExampleThree";
import HookExampleFour from "./HookExampleFour";
const HookExample = () => {
return (
<div>
<h1>State Hook的综合示例</h1>
<hr/>
<HookExampleOne/>
<hr/>
<h1>等价实现计数器</h1>
<HookExampleTwo/>
<hr/>
<h1>多个state值的应用开发</h1>
<HookExampleThree/>
<hr/>
<h1>state Hook的数组解构实现</h1>
<HookExampleFour/>
</div>
)
}
export default HookExample;
猜你喜欢
- 2024-11-24 React(6)React受控组件的使用
- 2024-11-24 React(8)React组件的生命周期
- 2024-11-24 React-EffectHook的应用简单示例
- 2024-11-24 业余时间总结了React的setState原理,有兴趣的了解下
- 2024-11-24 「react进阶」一文吃透React高阶组件(HOC)
- 2024-11-24 2022 年初级 React 开发人员的 5 大面试问题
- 2024-11-24 React-Redux简单计算示例
- 2024-11-24 手把手教你深入浅出React 迷惑的问题点【完整版】
- 2024-11-24 day2:前端面试题(react)
- 2024-11-24 阿里开源跨组件体系的表单渲染引擎——form-render
- 最近发表
- 标签列表
-
- gitpush (61)
- pythonif (68)
- location.href (57)
- tail-f (57)
- pythonifelse (59)
- deletesql (62)
- c++模板 (62)
- css3动画 (57)
- c#event (59)
- linuxgzip (68)
- 字符串连接 (73)
- nginx配置文件详解 (61)
- html标签 (69)
- c++初始化列表 (64)
- exec命令 (59)
- canvasfilltext (58)
- mysqlinnodbmyisam区别 (63)
- arraylistadd (66)
- node教程 (59)
- console.table (62)
- c++time_t (58)
- phpcookie (58)
- mysqldatesub函数 (63)
- window10java环境变量设置 (66)
- c++虚函数和纯虚函数的区别 (66)