小编Bil*_*ill的帖子

React TypeScript 将函数传递给子组件

我试图将一个函数传递给一个子组件,但我收到了一些打字稿错误......

父.tsx

import React from 'react';
import {Child} from './child';

const Parent: React.FC = () => {
    function fire() {
        console.log('fire')
    }

    return (
        <Child fire={fire}/>
//         ^___ error here!
    )
}
Run Code Online (Sandbox Code Playgroud)

错误fireType '{ fire: () => void; }' is not assignable to type 'IntrinsicAttributes & { children?: ReactNode; }'. Property 'fire' does not exist on type 'IntrinsicAttributes & { children?: ReactNode; }'.ts(2322)

孩子.tsx

import React from 'react';
const Child: React.FC = (props: any) => {
    return …
Run Code Online (Sandbox Code Playgroud)

typescript reactjs

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

React TypeScript:类型 'string[]' 不可分配给类型 'never[]'

当我将数组保存到某个状态然后再次读取它时,我收到错误 Type 'string[]' is notassignable to type 'never[]' 这是怎么回事?

我在这里制作了一个代码沙箱:https : //codesandbox.io/s/affectate-pasteur-kj5h0代码运行但以红色下划线显示错误

typescript reactjs

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

React TypeScript: (props: OwnProps) =&gt; Element' 不可分配给类型 'FunctionComponent&lt;{}&gt;

作为 JSX 元素,一切都很好......

interface OwnProps {
  children: JSX.Element;
  location: Location;
}

export function Layout(props: OwnProps): JSX.Element {
Run Code Online (Sandbox Code Playgroud)

当我将其更改为功能组件时,出现错误 Layout

interface OwnProps {
  children: JSX.Element;
  location: Location;
}

const Layout: React.FC = (props: OwnProps) => {
Run Code Online (Sandbox Code Playgroud)

错误是

Type '(props: OwnProps) => Element' is not assignable to type 'FunctionComponent<{}>'.
  Types of parameters 'props' and 'props' are incompatible.
    Property 'location' is missing in type '{ children?: ReactNode; }' but required in type 'OwnProps'.ts(2322)
layout.tsx(18, 3): 'location' is declared here.
Run Code Online (Sandbox Code Playgroud)

typescript reactjs

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

打字稿上传目录,类型上不存在属性“目录”

我有一个基本的文件输入,我想允许用户上传整个目录

      <input
        type='file'
        directory=''
        webkitdirectory=''
        className={cssClass}
        onChange={processFiles}
      />
Run Code Online (Sandbox Code Playgroud)

这给出了打字稿错误

Type '{ type: "file"; directory: string; webkitdirectory: string; className: string; }' is not assignable to type 'DetailedHTMLProps<InputHTMLAttributes<HTMLInputElement>, HTMLInputElement>'.
  Property 'directory' does not exist on type 'DetailedHTMLProps<InputHTMLAttributes<HTMLInputElement>, HTMLInputElement>'.ts(2322)
Run Code Online (Sandbox Code Playgroud)

typescript reactjs

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

需要Firebase数据结构建议

我想跟踪和报告聊天室中的用户,我不确定如何最好地构建Firebase中的数据.

概况

  • 每个用户都有一个唯一的user_id
  • 聊天室总是打开和关闭
  • 每个聊天室都有自己独特的room_id
  • 用户经常进入和离开打开的聊天室
  • 如果聊天室关闭,则用户无法进入房间
  • 一个用户可能同时在多个聊天室中

获取数据

我们可以访问返回json的API,我计划每1分钟查看一次API查找所有聊天室(room_id),然后请求每个房间的所有用户(user_id).

设置数据

数据的设置完全在我们的控制之下

报告我希望能够得到

  • 我们从x到y的日期和时间看到了多少个唯一身份用户
  • 从x到y日期和时间在线用于1位用户的时间

问题

  • firebase会为我记录每条记录吗?或者我是否需要将时间写入每条记录?
  • 最好使用unix Epoch还是更容易理解的日期时间?
  • 我应该如何在firebase中构建这些数据?

firebase

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

TypeScript:使用 reduce 传递对象数组的正确类型

我收到一个类型错误,我不知道如何正确构建界面

  const data = [
    { category: 'fish', age: 10, color: 'red' },
    { category: 'fish', age: 9, color: 'red' },
    { category: 'fish', age: 8, color: 'blue' },
    { category: 'fish', age: 7, color: 'blue' },
    { category: 'birds', age: 10, color: 'red' },
    { category: 'birds', age: 9, color: 'red' },
    { category: 'birds', age: 8, color: 'blue' },
    { category: 'birds', age: 7, color: 'blue' },
  ];

  interface CountProps {
    category: string;
    age: number;
    color: string;
  } …
Run Code Online (Sandbox Code Playgroud)

typescript

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

React TypeScript:类型“null”不可分配给类型“CanvasRenderingContext2D”

我似乎能够创建画布,但是当我获得上下文时

  const resizeImage = async (maxSize: number) => {
    const image = originalImage;
    const id = positionId;
    const resizeCanvas = document.createElement('canvas');
    resizeCanvas.width = maxSize;
    resizeCanvas.height = maxSize;
    const ctx: CanvasRenderingContext2D = resizeCanvas.getContext('2D');
//         ^ Error
    ...
Run Code Online (Sandbox Code Playgroud)

错误是

Type 'CanvasRenderingContext2D | ImageBitmapRenderingContext | WebGLRenderingContext | WebGL2RenderingContext | null' is not assignable to type 'CanvasRenderingContext2D'.
  Type 'null' is not assignable to type 'CanvasRenderingContext2D'.ts(2322)
Run Code Online (Sandbox Code Playgroud)

canvas typescript reactjs

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

Mongoose 在发送到前端之前删除 _id 和 __v

当我通过 mongoose 从 mongo 获取数据时......

      const allAssets = await assets
        .find({ company })
        .sort([['createdAt', -1]])
        .exec();
      res.status(200).json({ assets: allAssets });
Run Code Online (Sandbox Code Playgroud)

我总是得到 _id 和 __v 但我不想将它们发送到前端,有没有办法可以轻松地说我不想要这些值或在发送它们之前删除它们?

    {
      "indistructable": true,
      "_id": "5e345c2dc84be8995a5b4cf2",
      "baseUri": "https://api.website.com/nameco8/",
      "company": "jnameco8",
      "description": "asset",
      "image": "myimage.png",
      "name": "jim",
      "supply": 100,
      "createdAt": "2020-01-31T16:56:13.816Z",
      "updatedAt": "2020-01-31T16:56:13.816Z",
      "__v": 0
    },
Run Code Online (Sandbox Code Playgroud)

我尝试过添加

    __v: {type: Number, select: false},
    _id: { type: mongoose.Schema.Types.ObjectId, select: false },
Run Code Online (Sandbox Code Playgroud)

到我的架构,但是当保存到架构时,我收到错误 "document must have an _id before saving"

mongoose mongodb mongoose-schema

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

TypeScript 中的自定义 React 挂钩 - 没有调用签名

我有这个自定义钩子来调用 API

import { useState, useCallback } from 'react';

interface OptionsShape {
  method: 'GET' | 'POST';
}

interface InitStateShape {
  data: any;
  success: boolean;
  loading: boolean;
  error: Error | null;
}

const useAPI = (initialData = null) => {
  const initialState: InitStateShape = {
    data: initialData,
    success: false,
    loading: false,
    error: null,
  };

  const [response, setResponse] = useState(initialState);

  const callAPI = async (URL: string, options: OptionsShape) => {
    setResponse({ ...response, success: false, loading: true });

    try {
      const response = …
Run Code Online (Sandbox Code Playgroud)

typescript reactjs

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

fastify:流过早关闭

我正在尝试以流的形式发送图像......

  const { Readable } = require("stream");
  ...
  fastify.get(
    "/v1/files/:cid",
    async function (request: any, reply: any) {
      const { cid }: { cid: string } = request.params;
      const ipfs = create();
      const readableStream = Readable({
        async read() {
          for await (const chunk of ipfs.cat(cid)) {
            this.push(chunk);
          }
          this.push(null);
        },
      });
      reply.type("image/png").send(readableStream);
    }
  );
Run Code Online (Sandbox Code Playgroud)

也尝试过

      const readableStream = new Readable();
      readableStream.read = () => {};
      for await (const chunk of ipfs.cat(cid)) {
        readableStream.push(chunk);
      }
Run Code Online (Sandbox Code Playgroud)

我得到的信息是...

[17:31:38.006] INFO (44857): stream closed prematurely …
Run Code Online (Sandbox Code Playgroud)

node.js fastify

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