我useState用来管理状态,它工作正常。但是当我在它的内部返回状态时,它总是初始值
import react, {useState} from 'react'
const MyComponent = () => {
const [myState, setMyState] = useState({
value: 'initial value',
setValue: (newValue) => {
setMyState({...myState, value: newValue})
console.log(myState.value) //<--always is 'initial value'
}
})
return(
<>
<p>{myState.value}</p> //<-- it's working fine
<input value={myState.value} onChange={(e) => myState.setValue(e.target.value)} /> //<--working fine too
</>
)
}
Run Code Online (Sandbox Code Playgroud)
我期望console.log输入的值,但实际输出总是初始值
我正在尝试发布FormData包括 aFile和 a Collection。
这是我的模型:
public class Content
{
public string Name { get; set; }
public string Link { get; set; }
}
public class Model
{
public IEnumerable<Content> Contents { get; set; }
public IFormFile File { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
这是我的操作:
[HttpPost]
public async Task InsertAsync([FromForm]Model dataPost)
{
await _services.Create(dataPost);
}
Run Code Online (Sandbox Code Playgroud)
我FormData在 JavaScript 中是:
const formData = new FormData();
formData.append("File", myFile, "image.jpg")
formData.append("Contents", arrayOfContent)
Run Code Online (Sandbox Code Playgroud)
这是header:
'Content-Type': `multipart/form-data`
Run Code Online (Sandbox Code Playgroud)
“文件”工作正常,但“内容”始终为空。 …
我有一个像这样的简单动作:
[HttpGet]
public async Task<string> GetHtml()
{
Console.WriteLine("Run=======================================================");
await Task.Delay(5000);
Console.WriteLine("End=======================================================");
return "ok";
}
Run Code Online (Sandbox Code Playgroud)
我快速打开三个窗口,然后转到该 URL。我预计结果如下:
Run=======================================================
Run=======================================================
Run=======================================================
End=======================================================
End=======================================================
End=======================================================
Run Code Online (Sandbox Code Playgroud)
但是,事实并非如此!相反,结果是:
Run=======================================================
End=======================================================
Run=======================================================
End=======================================================
Run=======================================================
End=======================================================
Run Code Online (Sandbox Code Playgroud)
此外,当第一次运行完成后,第二次请求开始。为什么?
My problems are so hard to explain.
So, look at my code below.
I have a class model as follows:
public class MyModel{
public int ID{get;set;}
public List<string> CHILD{get;set;}
}
Run Code Online (Sandbox Code Playgroud)
I have a list of MyModel, and some of elements have same ID.
How can I create a new list from list of MyModel with unique ID and CHILD has combined?
Thank you so much