我有以下自定义挂钩,它将数据存储在本地存储中:
import { useCallback, useEffect, useState } from "react";
export const useLocalStorage = (key, initialValue) => {
const initialize = (key) => {
try {
const item = localStorage.getItem(key);
if (item && item !== "undefined") {
return JSON.parse(item);
}
localStorage.setItem(key, JSON.stringify(initialValue));
return initialValue;
} catch {
return initialValue;
}
};
const [state, setState] = useState(() => initialize(key)); // problem is here
const setValue = useCallback(
(value) => {
try {
const valueToStore = value instanceof Function ? value(storedValue) : value; …Run Code Online (Sandbox Code Playgroud) 目前我有一个AuthenticationController,它监听注册后。此注册方法存储用户并创建 jwt 令牌。
[HttpPost("registration")]
public async Task<IActionResult> Registration([FromBody] RegistrationViewModel model)
{
if (!ModelState.IsValid)
return BadRequest(new { code = "ModelNotValid", description = "Model is not valid." });
if (_userService.UserNameTaken(model.UserName))
return BadRequest(new { code = "UserNameTaken", description = $"The User Name
{model.UserName} is taken." });
var identityResult = await _userService.CreateUserAsync(model);
if (!identityResult.Succeeded)
return BadRequest(identityResult.Errors);
var user = _userService.GetUserByUserName(model.UserName);
int validityDurationInHours = 3;
string token = _jwtService.GenerateJwtToken(user, await _userService.GetUserRolesAsync(user),
validityDurationInHours);
return Ok(new { code = "RegistrationSuccess", auth_token = token …Run Code Online (Sandbox Code Playgroud) 通过组件ngOnInit方法中 Angular 中的路由,我通过 observable 获取流派 id,在该 observable 中,我调用一个带有发出 HTTP 请求的服务的方法。
this.movies: Movie[];
ngOnInit() {
this.route.paramMap.subscribe(param => {
let id = +param.get('id');
this.movieService.getMoviesByGenres(id).subscribe( response => {
this.movies = response['results'];
});
});
}
Run Code Online (Sandbox Code Playgroud)
它返回这个:
"results": [
{
"vote_count": 664,
"id": 287947,
"video": false,
"vote_average": 7.4,
"title": "Shazam!",
.
.
.
},
{
"vote_count": 3623,
"id": 299537,
"video": false,
"vote_average": 7.2,
"title": "Captain Marvel",
.
.
.
}, ...
]
Run Code Online (Sandbox Code Playgroud)
它返回的是没有演员表的电影,因此我需要通过为第一个请求返回的每部电影调用另一个 HTTP 请求来请求电影的演员表,并将第二个请求的演员表信息推送到数组movies[i].cast。
所以基本上我想要的看起来像这样:
ngOnInit() {
this.route.paramMap.subscribe(param => { …Run Code Online (Sandbox Code Playgroud) angular ×1
asp.net-core ×1
backend ×1
next.js ×1
observable ×1
react-hooks ×1
reactjs ×1
typescript ×1