小编Moh*_*bil的帖子

未捕获的语法错误:预期表达式,使用 Next.js 中间件时得到“<”

如果使用 Spotify 的 API 没有可用令牌,我将使用 Next.js 中间件重定向到登录页面,

我的中间件如下所示:

import { getToken } from "next-auth/jwt";
import { NextResponse } from "next/server";
import type { NextRequest } from "next/server";

export async function middleware(req: NextRequest) {
  //   const token = await getToken({ req, secret: process.env.JWT_SECRET });
  const token = await getToken({ req, secret: process.env.NEXTAUTH_SECRET });
  const { pathname, origin } = req.nextUrl;
  if (pathname.includes("/api/auth") || token) {
    return NextResponse.next();
  }
  //   console.log(origin);
  // console.log(token);

  if (!token && pathname != "/login") {
    return NextResponse.redirect(new …
Run Code Online (Sandbox Code Playgroud)

middleware reactjs next.js

11
推荐指数
4
解决办法
6482
查看次数

尽管我正在更改状态,但 React ui 没有更新

import React, { useState } from "react";

const App = () => {
  const anecdotes = [
    "If it hurts, do it more often",
    "Adding manpower to a late software project makes it later!",
    "The first 90 percent of the code accounts for the first 10 percent of the development time...The remaining 10 percent of the code accounts for the other 90 percent of the development time.",
    "Any fool can write code that a computer can understand. Good programmers write …
Run Code Online (Sandbox Code Playgroud)

javascript reactjs

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

如何在 Next.js 中使用 localStorage 并且不会出现水合错误?

我想使用本地存储来保存令牌并访问它,

我的代码如下所示:

if (typeof window !== 'undefined') {
    localStorage.setItem(key,value)
}
Run Code Online (Sandbox Code Playgroud)

如果我不使用检查窗口类型的部分,我会收到错误:

本地存储未定义

但这似乎导致文档中提到的错误,可以通过删除 typeof 窗口检查来修复该错误,因为当我刷新页面时,这似乎会导致水合错误:

Hydration 失败,因为初始 UI 与服务器上呈现的内容不匹配。

知道在这里做什么吗?

typescript reactjs next.js

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

如何使用 Yup 验证来验证 React 选择值?

我目前正在使用react-select和是的进行验证,反应选择组件的代码如下所示

import React, { useState, useMemo } from "react";
import Select from "react-select";
import countryList from "react-select-country-list";

function CountrySelector({ register }) {
  const [value, setValue] = useState("");
  const options = useMemo(() => {
    return countryList().getData();
  }, []);

  const changeHandler = (value) => {
    setValue(value);
    console.log(value);
  };
  // console.log(countryList().getData())
  return (
    <>
      <div className="mb-2.5">
        <Select
          placeholder="Select a Country"
          className=""
          options={options}
          value={value}
          {...register}
          onChange={changeHandler}
        />
      </div>
    </>
  );
}

export default CountrySelector;
Run Code Online (Sandbox Code Playgroud)

我的架构如下所示:

const schema = yup.object().shape({
  email: yup
    .string()
    .required("This field …
Run Code Online (Sandbox Code Playgroud)

reactjs react-select yup

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

在redux工具包中导入reducer是如何工作的?

所以我正在阅读 的文档rtk,所示的示例是使用以下代码创建切片之后:

import { createSlice } from '@reduxjs/toolkit'

const initialState = {
  value: 0,
}

export const counterSlice = createSlice({
  name: 'counter',
  initialState,
  reducers: {
    increment: (state) => {
      // Redux Toolkit allows us to write "mutating" logic in reducers. It
      // doesn't actually mutate the state because it uses the Immer library,
      // which detects changes to a "draft state" and produces a brand new
      // immutable state based off those changes
      state.value += 1
    },
    decrement: …
Run Code Online (Sandbox Code Playgroud)

javascript reactjs redux redux-toolkit

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

不良状态:无元素

我在浏览列表时遇到此错误,问题是列表不为空,因为我有一些手动数据放置它,所以我不确定我做错了什么,这是我的代码

class ProductDetailScreen extends StatelessWidget {
  // final String title;
  // final double price;

  // ProductDetailScreen(this.title, this.price);
  static const routeName = '/product-detail';

  @override
  Widget build(BuildContext context) {
    final String productId =
        ModalRoute.of(context).settings.arguments as String; // is the id!

    final loadedProduct = Provider.of<Products>(
      context,
      listen: false,
    ).findById(productId);

    // ...
    return Scaffold(
      appBar: AppBar(
        title: Text(loadedProduct.title),
      ),
    );
  }
}
Run Code Online (Sandbox Code Playgroud)

通过视图 id 查找方法来自这里:

    import 'package:flutter/material.dart';
import 'package:shop_app/providers/product.dart';

class Products with ChangeNotifier {
  List<Product> _items = [
    Product(
      id: 'p1',
      title: 'Red Shirt', …
Run Code Online (Sandbox Code Playgroud)

dart flutter

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

如何从表单提交时的反应查询将输入发送到 useMutation 挂钩?

我试图在表单提交上调用一个突变,用于登录用户,我试图调用的函数是这样的:

export const login = async (email : string , password : string) => {
  
  const response = await  axios.post(`${BASE_URL}/users/login` , {"user":{email:email, password:password}});

  return response.data;

}
Run Code Online (Sandbox Code Playgroud)

我正在尝试使用以下代码调用它:

  const { mutate, isLoading , error} = useMutation(variables => login(variables.email , variables.password))
Run Code Online (Sandbox Code Playgroud)

我提交的表单如下所示:

  onSubmit={(values) => {
    console.log(values);
    mutate({email : values.email , password:values.password});
  }} 
Run Code Online (Sandbox Code Playgroud)

但我这样做时遇到多个错误,正确的方法是什么?

错误是:类型“void”上不存在属性“email”。

类型“void”上不存在属性“password”。

类型为 '{ email: string; 的参数 密码:字符串;}' 不可分配给“void”类型的参数。

typescript reactjs formik react-query

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