import React, { useState} from "react";
import ReactDOM from "react-dom";
function App() {
const [count, setCount] = useState(0);
function handleAlertClick(){
return (setTimeout(() => {
alert("You clicked on: " + count);
}, 3000))
}
return (
<div>
<p>You clicked {count} times</p>
<button onClick={() => setCount(count + 1)}>Click me</button>
<button onClick={handleAlertClick}>Show alert</button>
</div>
);
}
Run Code Online (Sandbox Code Playgroud)
我只想知道这是否像我认为的那样有效,或者是否有更好的解释!
每当setState调用该方法时,状态都会获得一个新的引用。这意味着原始状态没有新值,而是我们用新值创建了一个新状态。当我们点击第二个按钮时,事件处理函数捕获原始状态的引用。即使我们多次单击第一个按钮,当显示警报时,它也会显示事件处理程序捕获其引用的状态值。
这样对吗?
我试图Map<String, List<String>>在Spring Boot编写的应用程序中创建一个类型的对象Kotlin.
我能够从config创建一个映射,并且还能够从config创建一个列表,但是当我尝试将两者合并时,我得到以下异常:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'myConfiguration': Could not bind properties to MyConfiguration (prefix=, ignoreInvalidFields=false, ignoreUnknownFields=true, ignoreNestedProperties=false); nested exception is java.lang.NullPointerException
Run Code Online (Sandbox Code Playgroud)
我的Configuration Object:
@ConfigurationProperties
@Component
class MyConfiguration {
var myNewMap: Map<String, List<String>>? = null
}
Run Code Online (Sandbox Code Playgroud)
我的Configuration yml:
---
myNewMap:
firstKey:
- 2
- 4
secondKey:
- 2
Run Code Online (Sandbox Code Playgroud)
这是否可以通过Spring读取配置的方式实现?或者是创建一个简单的Map的唯一方法,将值作为逗号分隔的字符串,并将其转换为我的应用程序中的列表?
Kotlin: 1.2.0
Spring Boot: 1.5.6.RELEASE
我想创建使用一个简单的HelloWorld应用程序kotlin,gradle该gradle这个和application插件.当我使用以下设置运行它时,我收到以下错误:
Error: Main method is not static in class com.petarkolaric.helloworld.Main, please define the main method as:
public static void main(String[] args)
Run Code Online (Sandbox Code Playgroud)
我的build.gradle:
group 'helloworld'
version '1.0-SNAPSHOT'
buildscript {
ext.kotlin_version = '1.2.0'
repositories {
mavenCentral()
}
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}
apply plugin: 'kotlin'
apply plugin: 'application'
mainClassName = "com.petarkolaric.helloworld.Main"
repositories {
mavenCentral()
}
dependencies {
compile "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
}
compileKotlin {
kotlinOptions.jvmTarget = "1.8"
}
compileTestKotlin {
kotlinOptions.jvmTarget = "1.8"
}
Run Code Online (Sandbox Code Playgroud)
我的 …
kotlin ×2
build.gradle ×1
closures ×1
gradle ×1
javascript ×1
jvm ×1
reactjs ×1
spring ×1
spring-boot ×1
use-state ×1
yaml ×1