react16.8之后推出了hook,函数式组件成为主流。
代码位置
phoenixhell\src\components
(1). State Hook让函数组件也可以有state状态, 并进行状态数据的读写操作
(2). 语法: const [xxx, setXxx] = React.useState(initValue)
(3). useState()说明:
参数: 第一次初始化指定的值在内部作缓存
返回值: 包含2个元素的数组, 第1个为内部当前状态值, 第2个为更新状态值的函数
(4). setXxx()2种写法:
setXxx(newValue): 参数为非函数值, 直接指定新的状态值, 内部用其覆盖原来的状态值
setXxx(value => newValue): 参数为函数, 接收原本的状态值, 返回新的状态值, 内部用其覆盖原来的状态值
let [variable1, variable2, ...] = array;
let numbers = [1, 2, 3, 4, 5];
let [first, second, third] = numbers;
console.log(first); // 输出: 1
console.log(second); // 输出: 2
console.log(third); // 输出: 3
你也可以忽略某些值:
let numbers = [1, 2, 3, 4, 5];
let [first, , third] = numbers;
console.log(first); // 输出: 1
console.log(third); // 输出: 3
你也可以在数组结构中使用其余参数:
let numbers = [1, 2, 3, 4, 5];
let [first, ...rest] = numbers;
console.log(first); // 输出: 1
console.log(rest); // 输出: [2, 3, 4, 5]
你可以为结构赋值提供默认值:
let numbers = [1];
let [first, second = 2] = numbers;
console.log(first); // 输出: 1
console.log(second); // 输出: 2,因为numbers中没有第二个元素,所以使用默认值2