当我在这个问题上进行搜索时,我只能找到this.state直接在方法体中某处修改而不是使用的问题this.setState().我的问题是我想在构造函数中设置一个起始状态,如下所示:
export default class Square extends React.Component<any, any> {
constructor(props: any) {
super(props);
this.state = {
active: false
};
}
public render() {
...
}
}
Run Code Online (Sandbox Code Playgroud)
该应用程序无法启动以下编译错误:
Cannot assign to 'state' because it is a constant or a read-only property
Run Code Online (Sandbox Code Playgroud)
这是因为在定义中React.Component我们有:
readonly state: null | Readonly<S>;
Run Code Online (Sandbox Code Playgroud)
所以我不确定该怎么做.JS中的官方反应教程直接指定this.state并表示在构造函数中这样做是可接受的模式,但我无法弄清楚如何使用TypeScript执行此操作.
我有几个具有几乎相同签名的函数(比实际代码短得多):
int hello(A a, B b, C c, int n);
int there(A a, B b, C c, int n);
int how(A a, B b, C c, int n);
int are(A a, B b, C c, int n);
...
Run Code Online (Sandbox Code Playgroud)
等等。然后在调用期间,代码创建一次参数,然后将相同的对象传递给每个函数,除了 n:
A a; B b; C c;
hello(a, b, c, 240);
there(a, b, c, 33);
how(a, b, c, 54);
are(a, b, c, 67);
Run Code Online (Sandbox Code Playgroud)
我想要实现的是类似于std::bind通常使用的东西,除了我想换掉功能。例如:
auto uber_func = std::something_stack_overflow_recommends(..., a, b, c)
uber_func(hello, 240);
uber_func(there, 33);
uber_func(how, 54);
uber_func(are, 67); …Run Code Online (Sandbox Code Playgroud) 我有以下辅助函数,它接受字符串视图并返回查找unordered_map:
int Scanner::getOpCount(std::string_view op) {
auto itr = Parser::opTable.find(op);
}
// in another file:
const static std::unordered_map<std::string, OpEntry> opTable;
Run Code Online (Sandbox Code Playgroud)
这不会编译,因为find需要一个字符串参数,所以我找到的唯一解决方案(如果我错了,请纠正我)是将其包装op为string{op}. 然而,让我担心的是,这是std::string_view为了更容易地传递字符串,但如果我必须从中构造一个字符串,而不管函数体内如何,那么使用getOpCount参数定义有什么优势吗string_view?或者这是否相当于(如果不是更慢的话)const std::string&?
我目前正在学习 Dart,但这也适用于现在 JavaScript 世界中发生的事情,而且 C# 似乎也使用相同的模式。
在 Dart 中,任何使用的函数await本身都必须通过async以下方式标记为异步:
import "dart:html";
main() async {
var context = querySelector("canvas").context2D;
var running = true;
while (running) {
var time = await window.animationFrame;
...
}
}
Run Code Online (Sandbox Code Playgroud)
这对我来说没有意义。如果一个函数正在等待一个异步函数完成,那么它不是被认为是阻塞的吗?为什么 JS 和 Dart 要求将其标记为异步?不会适得其反吧?
对我来说,如果调用函数在async调用任何在其定义中也包含它的函数时必须使用关键字,那将更有意义。在这种模式中,await将用于将异步函数转换为同步函数。
这种方式也可以避免重复的功能,因为现在,库似乎总是有func()andfuncSync()或funcAsync()。
我有一个数据结构,它本质上是一个链表,这意味着它的节点包含指向下一个节点的数据。
我研究过在这个类上使用 for 循环促进迭代,我发现必须实现 Iterable 接口。但是,我对如何实现 forEach() 和 spliterator() 以及每种方法的作用感到困惑。
实际上不可能创建这样的宏还是我做错了:
sample!("hello", "there") =>
println!("{:?}", "hello");
println!("{:?}", "there");
sample!("hello", "there", a "second type", a "another second type") =>
println!("{:?}", "hello");
println!("{:?}", "there");
println!("second {:?}", "second type");
println!("second {:?}", "another second type");
Run Code Online (Sandbox Code Playgroud)
我试过的是这个(游乐场链接):
macro_rules! sample {
(
$( $first:literal ),*
$( a $second:literal ),*
) => {
$(
println!("{:?}", $first);
)*
$(
println!("second {:?}", $second);
)*
};
}
Run Code Online (Sandbox Code Playgroud)
哪个失败了:
error: no rules expected the token `a`
--> main.rs:18:20
|
1 | macro_rules! sample {
| ------------------- when calling this …Run Code Online (Sandbox Code Playgroud) 尝试编写构建文件时,我一直遇到cmake的问题。该find_package命令应该定义要与include_directories指令一起使用的变量,但是这样做的前后不一致。例如,考虑以下代码:
find_package(OpenGL REQUIRED)
find_package(glm REQUIRED)
message(STATUS ${GLM_INCLUDE_DIR})
message(STATUS ${GLM_INCLUDE_DIRS})
message(STATUS ${OPENGL_INCLUDE_DIR})
message(STATUS ${OPENGL_INCLUDE_DIRS})
Run Code Online (Sandbox Code Playgroud)
message即使第二张和第三张为“ DIR”,另一张为“ DIRS”,也仅打印第二张和第三张
有没有一种标准的方法来确定您应该使用哪个?