小编J.D*_*Doe的帖子

Next.js 使用 SSR 的本地存储问题

我有以下自定义挂钩,它将数据存储在本地存储中:

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)

reactjs server-side-rendering next.js react-hooks

9
推荐指数
2
解决办法
2万
查看次数

创建用户和颁发 jwt 令牌的干净方法?

目前我有一个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)

authentication backend asp.net-web-api asp.net-core

5
推荐指数
0
解决办法
389
查看次数

迭代由 observable 提供的对象数组并调用 http 请求 foreach object.id?

通过组件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)

observable typescript angular

1
推荐指数
1
解决办法
4139
查看次数