简体   繁体   English

类型错误:links.map 不是 function 反应

[英]TypeError: links.map is not a function React

I'm working with reactjs typescript and cannot seem to prevent this error when trying to display JSON data from the server:我正在使用 reactjs typescript 并且在尝试显示来自服务器的 JSON 数据时似乎无法阻止此错误:

import React, { useRef, useState } from "react";
import useHttp from "../hooks/usehttp";

const HomePage: React.FC = () => {
const [links, setLinks] = useState([]);
const ref = useRef<HTMLInputElement>(null);
const { request } = useHttp();

const pressHandler = async (event: React.KeyboardEvent) => {
if (event.key === "Enter") {
  try {
    const data = await request(
      "http://localhost:5000/api/link/generate-guest",
      "POST",
      { from: ref.current!.value },
      {}
    );
    alert(data.message);
    console.log(data.link.to); // the link => localhost:5000/jshdadsa
    setLinks(data.link.to);
    console.log(links); // shows undefined ?????
  } catch (error) {
    alert(error);
  }
}
};

return (
<>
  <div className="row">
    <div className="col s8 offset-s2" style={{ paddingTop: "2rem" }}>
      <div className="input-field">
        <input
          placeholder="Enter link"
          id="link"
          type="text"
          ref={ref}
          onKeyPress={pressHandler}
        />
        <label htmlFor="link">Enter link</label>
      </div>
      <div>
        { {links.map((link: any) => {
          return (
            <ul className="collection with-header">
              <li className="collection-item">{link}</li>
            </ul>
          );
        })} }
      </div>
    </div>
  </div>
</>
);
};

export default HomePage;

Not sure what i am missing.不知道我错过了什么。

Use this code in order to update the array of link in your example:使用此代码来更新示例中的链接数组:

setLinks([...links, data.link.to])

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

相关问题 TypeError:this.state.user.map不是React中的函数 - TypeError: this.state.user.map is not a function in react 反应类型错误 this.state.data.map 不是 function - React TypeError this.state.data.map is not a function × TypeError: _questions_json__WEBPACK_IMPORTED_MODULE_1__.map 不是 function(反应) - × TypeError: _questions_json__WEBPACK_IMPORTED_MODULE_1__.map is not a function (react) 如何在React中修复“ TypeError:category.map不是函数”错误 - How to fix “TypeError: categories.map is not a function” error in React React JS - 未捕获的类型错误:this.props.data.map 不是一个函数 - React JS - Uncaught TypeError: this.props.data.map is not a function 类型错误:国家。map 不是 function - TypeError: countries.map is not a function 类型错误:this.state.persons.map 不是 function 使用 Z38C37877939C7B0B867C - TypeError: this.state.persons.map is not a function while using axios.get in React 尝试在 React 中从 API 获取数据 - TypeError: recipes.map is not a function - Trying to fetch data from API in React - TypeError: recipes.map is not a function JSON &amp; REACT 16.8 TypeError: _data_projects_json__WEBPACK_IMPORTED_MODULE_2__.map 不是 ZC1C425268E68385D1AB5074C17A - JSON & REACT 16.8 TypeError: _data_projects_json__WEBPACK_IMPORTED_MODULE_2__.map is not a function TypeError:data.map不是函数 - TypeError: data.map is not a function
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM