09-08-2021
// Instead of confusing React with code like this:
if (userName !== '') {
useEffect(() => {
localStorage.setItem('savedUserName', userName);
});
}
// We can accomplish the same goal, while consistently calling our Hook every time:
useEffect(() => {
if (userName !== '') {
localStorage.setItem('savedUserName', userName);
}
});
const [color, setColor] = useState();
当前状态(current state) - 范例中的color
更新状态的函数(state setter) - 范例中的setColor
import React, { useState } from 'react';
export default function ColorPicker() {
const [color, setColor] = useState();
const divStyle = {backgroundColor: color};
return (
<div style={divStyle}>
<p>The color is {color}</p>
<button onClick={() => setColor('Aquamarine')}>
Aquamarine
</button>
<button onClick={() => setColor('BlueViolet')}>
BlueViolet
</button>
<button onClick={() => setColor('Chartreuse')}>
Chartreuse
</button>
<button onClick={() => setColor('CornflowerBlue')}>
CornflowerBlue
</button>
</div>
);
}
import React, { useState } from 'react';
export default function ColorPicker() {
const [color, setColor] = useState();
const [color, setColor] = useState();
import React, { useState } from 'react';
const colorNames = ['Aquamarine', 'BlueViolet', 'Chartreuse', 'CornflowerBlue', 'Thistle', 'SpringGreen', 'SaddleBrown', 'PapayaWhip', 'MistyRose'];
export default function ColorPicker() {
const [color, setColor] = useState("Tomato");
const divStyle = {backgroundColor: color};
return (
<div style={divStyle}>
<p>Selected color: {color}</p>
{colorNames.map((colorName)=>(
<button
onClick={() => setColor(colorName)}
key={colorName}>
{colorName}
</button>
))}
</div>
);
}
在初次页面使用 "Tomato" 作为初始化的页面颜色
export default function ColorPicker() {
const [color, setColor] = useState("Tomato");
const divStyle = {backgroundColor: color};
常见的模式
const handleChange = (event) => {
const newEmail = event.target.value;
setEmail(newEmail);
}
也可以这样简化
const handleChange = (event) => setEmail(event.target.value);
或是使用解构的方式
const handleChange = ({target}) => setEmail(target.value);
范例:
import React, { useState } from "react";
// regex to match numbers between 1 and 10 digits long
const validPhoneNumber = /^\d{1,10}$/;
export default function PhoneNumber() {
const [phone, setPhone] = useState('');
const handleChange = ({ target })=> {
const newPhone = target.value;
const isValid = validPhoneNumber.test(newPhone);
if (isValid) {
setPhone(newPhone);
}
// just ignore the event, when new value is invalid
};
return (
<div className='phone'>
<label for='phone-input'>Phone: </label>
<input value={phone} onChange={handleChange} id='phone-input' />
</div>
);
}
在value 有更动的时候会去执行onChang{handleChange}
<input value={phone} onChange={handleChange} id='phone-input' />
</div>
import React, { useState } from 'react';
export default function QuizNavBar({ questions }) {
const [questionIndex, setQuestionIndex] = useState(0);
const goBack = () =>
setQuestionIndex((prevQuestionIndex) => prevQuestionIndex - 1);
const goToNext = () =>
setQuestionIndex((prevQuestionIndex) => prevQuestionIndex + 1);
const onFirstQuestion = questionIndex === 0;
// 起始值是0
const onLastQuestion = questionIndex === questions.length - 1;
// 回到上一个问题,等於问题-1
return (
<nav>
<span>Question #{questionIndex + 1}</span>
<div>
<button onClick={goBack} disabled={onFirstQuestion}>
// 点击上一步,或是在等於0的时候无法使用
Go Back
</button>
<button onClick={goToNext} disabled={onLastQuestion}>
// 点击往下一步,或是在最後一个无法使用
Next Question
</button>
</div>
</nav>
);
}
>>: D8 - 如何用 Google Apps Script 将 Google Calendar 上的事件与更新全部列出到 Google Sheet 上?
在[day 7]使用form tag 来进行submit的时候,不知道大家心里会不会有个疑问? 我要...
突然回到CSS好像有点跳tone,主要是因为在CSS & Javascript的基础不够熟练...
SG-90转起来! 今天要来玩伺服马达~ 我们使用 SG-90 透过PWM控制 精准控制角度从0° ...
今天我们来配 OSPF。 OSPF 是一种 IGP (Interior Gateway Protoc...
本系列文章同步发布於笔者网站 在经过了好几篇的 Neutron 之後我们终於来到下一个 OpenSt...