简体   繁体   English

为什么 JavaScript 中的一段有效代码会在 Z558B544CF685F338B64E 中产生 map function 错误?

[英]Why does a valid piece of code in JavaScript produce a map function error in TypeScript & React?

At this moment I'm trying to build a frontend page with a combination of React and TypeScript.目前我正在尝试使用 React 和 TypeScript 的组合构建前端页面。 What I'm trying to do is to fetch the "words" via a get method of axios and assign the data to an array, and then display it on the frontend page, which is fairly simple.我要做的是通过 axios 的 get 方法获取“单词”并将数据分配给数组,然后将其显示在前端页面上,这非常简单。 I've already made sure that the backend part works fine using Postman.我已经确保后端部分使用 Postman 可以正常工作。

As I didn't find a good "React & TypeScript" tutorial that exactly matches what I want to do, I'm studying with a "React & JavaScript" tutorial.由于我没有找到与我想做的完全匹配的好的“React 和 TypeScript”教程,所以我正在学习“React 和 JavaScript”教程。 What I find a little odd is that the completely valid part written in "React & JavaScript" causes an error "TypeError: this.state.wordsData.map is not a function" in my case, which is written in "React & TypeScript".我觉得有点奇怪的是,在我的情况下,用“React & JavaScript”编写的完全有效的部分会导致错误"TypeError: this.state.wordsData.map is not a function" ,它是用“React & TypeScript”编写的.

Here is my code.这是我的代码。

import React from 'react';
import axios from 'axios'
import Word from '../interfaces/Word.interface';

class Home extends React.Component{

  state = {
    wordsData:new Array<Word>()
  }

  componentWillMount(){
    axios.get('http://localhost:8080/pictionarizerservices/api/words')
    .then(res => {
      const data = res.data;
      console.log("The content of res: ");
      console.log(res);
      this.setState({
        wordsData:{
          id: data.id,
          ownLangWordName: data.ownLangWordName,
          targetLangWordName: data.targetLangWordName,
          ownLangExSentence: data.ownLangExSentence,
          targetLangExSentence: data.targetLangExSentence,
          createdDate: data.createdDate
        }
      })
    })
  }

  render(){
    return(
      <div>
        <h2>Words:</h2>
        <table>
        <thead>
          <tr>
          <th>ID</th>
          <th>Word (OL)</th>
          <th>Word (TL)</th>
          <th>Sentence (OL)</th>
          <th>Sentence (TL)</th>
          <th>Created Date</th>
          </tr>
        </thead>
        <tbody>
          {this.state.wordsData.map(singleWord=>
          <RowCreator 
            id={singleWord.id}
            ownLangWordName={singleWord.ownLangWordName}
            targetLangWordName={singleWord.targetLangWordName}
            ownLangExSentence={singleWord.ownLangExSentence}
            targetLangExSentence={singleWord.targetLangExSentence}
            createdDate={singleWord.createdDate}
            />)}
        </tbody>
        </table>
      </div>
    )
  }
}

class RowCreator extends React.Component<Word>{

  render(){
    let word = this.props;
    return(
        <tr>
          <td>{word.id}</td>
          <td>{word.ownLangWordName}</td>
          <td>{word.targetLangWordName}</td>
          <td>{word.ownLangExSentence}</td>
          <td>{word.targetLangExSentence}</td>
          <td>{word.createdDate}</td>
        </tr>
    )
  }
}

export default Home; 

I modified the syntax a little bit in order to adjust to TypeScript, but the followings are all I changed.为了适应TypeScript,我稍微修改了语法,但以下都是我改变的。

from

state = {
    patientData:[]
}

to

  state = {
    wordsData:new Array<Word>()
  }

,and from ,并且从

class RowCreator extends React.Component{
...

to

class RowCreator extends React.Component<Word>{
...

That's all.就这样。

What I already know by studying by myself about TypeScript is that TypeScript is a strict superset of JavaScript, and all valid code written in JavaScript should be valid in TypeScript as well. What I already know by studying by myself about TypeScript is that TypeScript is a strict superset of JavaScript, and all valid code written in JavaScript should be valid in TypeScript as well. Also, especially when writing in Visual Studio Code, TypeScript's error detecting feature is excellent and you'll find a lot more errors beforehand compared to when you are writing in JavaScript.此外,尤其是在 Visual Studio Code 中编写时,TypeScript 的错误检测功能非常出色,与在 JavaScript 中编写时相比,您会事先发现更多错误。 I made sure that all the error markups were gone before running the program, and now I get this "TypeError: this.state.wordsData.map is not a function" error as a run time error.我确保在运行程序之前所有的错误标记都消失了,现在我得到这个"TypeError: this.state.wordsData.map is not a function"错误作为运行时错误。

How can I get the part我怎样才能得到零件

<tbody>
          {this.state.wordsData.map(singleWord=>
          <RowCreator 
            id={singleWord.id}
            ownLangWordName={singleWord.ownLangWordName}
            targetLangWordName={singleWord.targetLangWordName}
            ownLangExSentence={singleWord.ownLangExSentence}
            targetLangExSentence={singleWord.targetLangExSentence}
            createdDate={singleWord.createdDate}
            />)}
        </tbody>

to work in TypeScript?在 TypeScript 工作?

you are setting it to an object您将其设置为 object

  this.setState({
    wordsData:{
      id: data.id,
      ownLangWordName: data.ownLangWordName,
      targetLangWordName: data.targetLangWordName,
      ownLangExSentence: data.ownLangExSentence,
      targetLangExSentence: data.targetLangExSentence,
      createdDate: data.createdDate
    }
  })

set it to an array like so:将其设置为这样的数组:

  this.setState({
    wordsData:[{
      id: data.id,
      ownLangWordName: data.ownLangWordName,
      targetLangWordName: data.targetLangWordName,
      ownLangExSentence: data.ownLangExSentence,
      targetLangExSentence: data.targetLangExSentence,
      createdDate: data.createdDate
    }]
  })

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM