在 React-TypeScript 中传递道具

Passing props in React-TypeScript

我正在使用 TypeScript 学习 React,并且正在构建一个简单的杂务记录器(用户在其中输入杂务信息,然后将其记录为新文档)。

因此,用户输入他们的杂务详细信息,点击提交,并在提交时创建一个新的 choreDoc 对象来保存他们的所有信息。该对象还包含一个方法 format(),该方法将用户信息作为字符串 returns。 我想将整个对象作为道具传递给 Document.tsx 组件,然后在该组件中提取 format() 函数,并呈现返回的数据。我只是对如何执行此操作感到困惑,当我尝试将 choreDoc 对象作为道具传递时,我收到以下 linting 错误:

Type '{ document: {}; }' is not assignable to type 'IntrinsicAttributes & Props & { children?: ReactNode; }'.
  Property 'document' does not exist on type 'IntrinsicAttributes & Props & { children?: ReactNode; 

有什么解决办法吗?

输入和提取用户信息的主要父组件: App.tsx

import React, {useState} from 'react';
import Document from './Document'
import './styles.css'

interface Formatter {
  format(): string
}

class choreDoc implements Formatter  {
  name:string
  chore:string
  date:string

  constructor(n:string,c:string,d:string){
    this.name = n
    this.chore = c
    this.date = d
  }

  format(){
    return `${this.name} completed this following chore: ${this.chore} on the following date: ${this.date}`
  }
}

function App() {
  const [name, setName] = useState('')
  const [chore, setChore] = useState('')
  const [date, setDate] = useState('')
  const [document, setDocument] = useState({})

  const handleNameChange = (e:React.FormEvent<HTMLInputElement>) => {
      e.preventDefault()
      setName(e.currentTarget.value)
  }
  const handleChoreChange = (e:React.FormEvent<HTMLInputElement>) => {
      e.preventDefault()
      setChore(e.currentTarget.value)
  }
  const handleDateChange = (e:React.FormEvent<HTMLInputElement>)=> {
      e.preventDefault()
      setDate(e.currentTarget.value)
  }
  const handleSubmit = (e: React.FormEvent<HTMLFormElement>)=> {
    e.preventDefault()
    let doc:Formatter = new choreDoc(name,chore,date)
    let arr = []
    arr.push(doc)
    setDocument(arr)
  }

  return(
    <>
      <div>
          <form className = 'input-list' onSubmit = {handleSubmit} >
              <label>
              Enter Name <br></br>
              <input type = 'text' name = 'name' onChange = {handleNameChange}></input>
              </label>
              <label>
              Chore <br></br>
              <input type = 'text' name = 'chore' onChange = {handleChoreChange}></input>
              </label>
              <label>
              Date completed <br></br>
              <input type = 'text' name = 'date' onChange = {handleDateChange}></input>
              </label>
              <div>
              <button type = 'submit' >Submit</button>
              </div>
          </form>
      </div>
      <div>
        <Document document = {document}/>
      </div>
      </>
  )
}

export default App;

Document.tsx 我想渲染从 format()

返回的信息
import React from 'react'

interface Props {
    format():string
}

const Document: React.FC<Props> = () => {
    return(
        <div className = 'doc'>
        </div>
    )
}

export default Document

通过将 document prop 作为 Formatter 放在 Props 接口中,并将 document 直接放在状态而不是数组中,它将按预期工作。

import React, { useState } from "react";

interface Props {
  document: Formatter;
}

const Document: React.FC<Props> = ({ document }) => {
  return <div className="doc">{document.format()}</div>;
};

interface Formatter {
  format(): string;
}

class ChoreDoc implements Formatter {
  name: string;
  chore: string;
  date: string;

  constructor(n: string, c: string, d: string) {
    this.name = n;
    this.chore = c;
    this.date = d;
  }

  format() {
    return `${this.name} completed this following chore: ${this.chore} on the following date: ${this.date}`;
  }
}

function App() {
  const [name, setName] = useState("");
  const [chore, setChore] = useState("");
  const [date, setDate] = useState("");
  const [document, setDocument] = useState<ChoreDoc | null>(null);

  const handleNameChange = (e: React.FormEvent<HTMLInputElement>) => {
    e.preventDefault();
    setName(e.currentTarget.value);
  };
  const handleChoreChange = (e: React.FormEvent<HTMLInputElement>) => {
    e.preventDefault();
    setChore(e.currentTarget.value);
  };
  const handleDateChange = (e: React.FormEvent<HTMLInputElement>) => {
    e.preventDefault();
    setDate(e.currentTarget.value);
  };
  const handleSubmit = (e: React.FormEvent<HTMLFormElement>) => {
    e.preventDefault();
    let doc = new ChoreDoc(name, chore, date);

    setDocument(doc);
  };

  return (
    <>
      <div>
        <form className="input-list" onSubmit={handleSubmit}>
          <label>
            Enter Name <br></br>
            <input type="text" name="name" onChange={handleNameChange}></input>
          </label>
          <label>
            Chore <br></br>
            <input
              type="text"
              name="chore"
              onChange={handleChoreChange}
            ></input>
          </label>
          <label>
            Date completed <br></br>
            <input type="text" name="date" onChange={handleDateChange}></input>
          </label>
          <div>
            <button type="submit">Submit</button>
          </div>
        </form>
      </div>
      <div>{document && <Document document={document} />}</div>
    </>
  );
}

export default App;