- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在 Typescript
中有此代码:
const [history, setHistory] = useState([Array(9).fill(null)]);
const newHistory = history.slice(0, currentStep + 1);
当我想使用像这样的扩展运算符设置新状态时:
setHistory(...newHistory);
我有错误:
A spread argument must either have a tuple type or be passed to a rest parameter.
最佳答案
使用时 useState
, 索引 1
在返回的元组中是 setHistory
.那应该只接受一个参数(新值)。原始帖子中的示例将历史扩展为潜在的 8 个参数。它需要作为一个数组传递,它可以像 setHistory(newHistory)
一样简单.
如果目的是总是修改以前的状态,我会打电话setHistory(previousHistory => previousHistory.slice(x, y))
.
还!对于 Typescript,你需要像这样为 useState 声明一个类型 useState<History[]>(Array(9).fill(null));
在不声明类型的情况下, typescript 将推断出一些在这种情况下可能不是您想要的东西。
请注意,您并不总是需要为 useState 声明类型。实际上,我鼓励尽可能使用类型推断。如果初始值为 null 或 undefined,您将无法在 Typescript 中使用类型推断。
关于reactjs - spread 参数必须具有元组类型或传递给 rest 参数 React,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68768781/
关于这两个插件的文档中没有太多详细信息,我知道它们都使 {...obj} 之类的语法有效。 但是我如何决定使用哪个插件呢? 最佳答案 我们在这里有相关文档:https://babeljs.io/doc
我的状态如下所示: state: { 1: {show: false, description: 'one'}, 2: {show: false, description: 'two'
我刚刚尝试启动 spread用于我用于集成不同传感器数据流程的一些工具的通信。 启动传播退出后立即显示以下消息: Conf_load_conf_file: using file: spread.con
这个问题在这里已经有了答案: Transpose / reshape dataframe without "timevar" from long to wide format (9 个回答) 关闭3年
我有这样的默认状态: this.state = { location:{ lat: 1234, lng: 3245 } } 所以每次我想更新纬度或经度时我都必须这样做 this
我正在使用 yeoman 生成器 react-webpack .我正在尝试在 Main.js 组件中使用传播属性: require('normalize.css'); require('styles/
我想知道是否可以使用 Spread 运算符在数组中添加具有特定键的元素。 let foo = ['a','b','c']; foo = [...foo,'d']; 在这种情况下,foo[3] 将是 d
开发数据库时,方法spread将数组值转换为命名参数是必不可少的。 但是当解析的数据无法直接映射时如何做同样的事情呢? 以下是嵌套事务的简单示例: db.tx(function () { re
我正面临如下描述的算法问题:给定一条从 0 到 N(非常大的 N)的直线,该直线上的 X 点列表,以及一个数字 Z(0= X 简化为 O(XlogN)。 关于algorithm - 从一条线上的点中选
我正在从一本书中学习 react ,在一个示例中,作者以这种方式制作了一个组件 return ( {[...Array(totalStars)].map((n,i)=
假设我有一个数字列表: some_numbers = [16.0, 16.01, 24.53, 22.99, 22.72, 22.71, 22.2, 21.36, 21.34, 21
几天来我对浅拷贝和深拷贝的真正定义感到非常困惑。 当我阅读关于浅拷贝的 mdn 文档 ( https://developer.mozilla.org/en-US/docs/Glossary/Shall
我有一个包含两列的数据集:第一列名为“键”并包含日期时间(尽管当前采用字符形式),第二列包含更多日期时间值。我想使用 spread 使键行成为列名。有点复杂,但一旦完成,我会将 tibble 转换为列
我们有以下数据框 a,内容如下: > a google_prod Value 1 categoria ML 2 google
在使用spread函数时,如何在保持数字排名的情况下进行扩容? library(tidyverse) data.frame(time = paste0("t_", 1:100)) %>% roww
我认为这已被多次询问,但我找不到合适的词来找到可行的解决方案。 我该怎么办 spread()基于多个键的多个值的数据框? 我正在处理的简化(我有更多的列要传播,但只有两个键:Id 和 time 给定测
考虑这个简单的例子 mydf mydf # A tibble: 2 x 3 regular_col normal_col weird_col 1
我需要使用 axios 处理未知数量的 AJAX 请求(1 个或更多),并且我不确定如何处理响应。我想要一些类似的东西: let urlArray = [] // unknown # of urls
这个问题已经有答案了: What are these three dots in React doing? (23 个回答) 已关闭 4 年前。 根据我的理解,这就是展开运算符的工作原理: x=[1,
我有 R data.frames 有不同数量的列。最后一列是数字,其余是字符串。我需要对它们进行排列,以便展开最后一个非数字列。困难在于我想以编程方式执行此操作,因为我不知道列名。 例如 df <-
我是一名优秀的程序员,十分优秀!