简体   繁体   English

我如何 map 反应 js 中的一个数组我试过了但是有一些问题谁能帮我

[英]how do i map an array in react js i have tried but there is some problem can anyone help me

So I try to get data from localDataStorage and try to populate on screen in react but the react is showing error `[{',' expected" in place of '.'所以我尝试从 localDataStorage 获取数据并尝试在屏幕上填充反应,但反应显示错误'[{','预期'代替'。' in jsx line localStorageData.iength or if i remove this from code than it's showing same error for 'localDataStorage.map' it asking me to remove '.'在 jsx 行 localStorageData.iength 中,或者如果我从代码中删除它而不是显示与“localDataStorage.map”相同的错误,它要求我删除“。”

import { useEffect, useState } from "react";
import "./inbox.css";
 
const Inbox = () => {
  const [localStorageData, setlocalStorageData] = useState([])

  useEffect =(()=>{
    JSON.parse(localStorage.getItem('todos')).then(data=> setlocalStorageData(data))
  }, [localStorageData])

  return (
    {localStorageData.length > 0 &&
       localDataStorage.map((element, index) => (
        <div className="inbox" key ={index}>
          <div className="bullet">&#x27A3;</div>
          <div className="msg">{element.message}</div>
          <div className="btn-container">
            <div className="date">{element.title}</div>
            <div className="edit btn hide">
              <i className="fa-solid fa-pen-to-square"></i>
            </div>
            <div className="delete btn hide">
              <i className="fa-solid fa-trash"></i>
            </div>
          </div>
        </div>
      ))
    }
  )
}

export default Inbox;

the error is as following错误如下

[{ "resource": "/C:/Users/PC/Desktop/web/new/src/component/inbox/inbox.js", "owner": "typescript", "code": "1005", "severity": 8, "message": "',' expected.", "source": "ts", "startLineNumber": 14, "startColumn": 22, "endLineNumber": 14, "endColumn": 23 }]

Remove the outer braces in your return statement.删除 return 语句中的外大括号。 Braces like this are only used inside JSX.像这样的大括号只在 JSX 内部使用。

Also, you probably meant .length instead of .iength .另外,您的意思可能是.length而不是.iength

i believe there should be localStorageData.map and not localData.map我相信应该有 localStorageData.map 而不是 localData.map

import React from "react"
import { useEffect, useState } from "react"
import "./inbox.css"

const Inbox = () => {
  const [localStorageData, setlocalStorageData] = useState([])

  useEffect =
    (() => {
      JSON.parse(localStorage.getItem("todos"))
        .then(data => setlocalStorageData(data))
        .catch(err => console.log(err))
    },
    [localStorageData])

  return (
    <>
      {localStorageData.length > 0 &&
        localStorageData.map((element, index) => (
          <div className="inbox" key={index}>
            <div className="bullet">&#x27A3;</div>
            <div className="msg">{element.message}</div>
            <div className="btn-container">
              <div className="date">{element.title}</div>
              <div className="edit btn hide">
                <i className="fa-solid fa-pen-to-square"></i>
              </div>
              <div className="delete btn hide">
                <i className="fa-solid fa-trash"></i>
              </div>
            </div>
          </div>
        ))}
    </>
  )
}

export default Inbox

you are not wrapping code into single element try您没有将代码包装到单个元素中尝试

return (
    <>
    {
      localStorageData.length > 0 &&
        localData.map((element, index) => (
          <div className="inbox" key={index}>
            <div className="bullet">&#x27A3;</div>
            <div className="msg">{element.message}</div>
            <div className="btn-container">
              <div className="date">{element.title}</div>
              <div className="edit btn hide">
                <i className="fa-solid fa-pen-to-square"></i>
              </div>
              <div className="delete btn hide">
                <i className="fa-solid fa-trash"></i>
              </div>
            </div>
          </div>
        ))
    }
    </>
  )

暂无
暂无

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

相关问题 我尝试了多种方法,谁能告诉我这个数组有什么问题? - I have tried multiple ways, Can anyone tell me what's wrong with this array? 我不知道如何解决这个问题,你能帮我吗? - I have no idea how to solve this problem, can you help me? 任何人都可以帮助我如何在流星反应应用程序中使用jQuery-formBuilder插件 - Can anyone help me how can I use jQuery-formBuilder plugin in meteor react app 我尝试创建一个图像弹出窗口但在这里我的脚本严重失败任何人都可以帮助我 - I tried creating a image popup but failed badly here my script anyone can help me out React.js 我在 prod 中有 Bug,但在 dev 模式下没有,你能帮帮我吗 - React.js i have Bug in prod but not in dev mode, can you help me 谁能帮我在这个 React JS 组件中隐藏 setTimeout id? - Can anyone help me to hide the setTimeout id in this React JS component? 任何人都可以帮我解决我在EventHandler上做错了什么因为某些原因我无法打开Display Conformation框? - Can anyone please help me what I did wrong on my EventHandler for some reason I can't open the Display Conformation box? 任何人都可以帮助我如何根据时间对 div 进行排序吗? js - Can anyone help me on how to sort div according to time? js javascript我怎么解决这个问题,请帮帮我 - How can i solve this problem in javascript, please help me 谁能帮我告诉我为什么我在函数中得到一个空值 - Can anyone help and tell me why do i get a null value in the function
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM