【问题标题】:map() is not rendering html elementsmap() 没有渲染 html 元素
【发布时间】:2021-11-03 18:31:59
【问题描述】:

我在这个问题上花了 4 个小时,但无法弄清楚问题所在。通过 props 我传递了一个数组(props.content)并想用<h4> tag(不具体)呈现每个元素,但我不能。当我尝试 console.log 数组的每个元素时,它会起作用。

import classes from "./Memes.module.css";
const Memes = (props) => {
        
       return (
         <div className={classes.memes__body}>
         {Array.prototype.forEach.call(props.content,items=>{
             console.log(items)
         })}
          </div> 
       );
}


export default Memes;

输出 -

但是当我运行相同的方法来渲染某些 html tags 中的所有项目时它不起作用。

import classes from "./Memes.module.css";

const Memes = (props) => {
        
       return (
         <div className={classes.memes__body}>
         {Array.prototype.forEach.call(props.content,items=>{
             <h1>{items}</h1>
         })}
          </div> 
       );
}


export default Memes;

输出 没用。

注意 - 这里props.content 是一个字符串数组。

Fetch_memes.js(父级)

import { useState } from "react";
import { useEffect } from "react";
import Memes from "./Memes";

const Fetch_memes = () => {

    const [image, setImage] = useState();
    const [text, setText] = useState("");

    useEffect(() => {
        const memes = async () => {
        const response = await  fetch('https://www.reddit.com/r/memes.json');
    
        if (!response.ok) {
          throw new Error("Something went wrong");
        }
    
        const responseData = await response.json();
        const img = responseData.data.children[0].data.thumbnail;
        const memesCollection = [];
        memesCollection.push("If you can Code then this doesn't mean that your are developer");

        for(let i=0 ; i<20 ; i++){
            memesCollection.push(responseData.data.children[i].data.title);
        }
        
        console.log(memesCollection[1]);
        setImage(img);
        setText(memesCollection);
        }
        memes();
      }, []);

  return (
      <Memes src={image} content={text}/>
  );
}

export default Fetch_memes;

【问题讨论】:

  • 同样的问题!请有人帮忙

标签: arrays reactjs mapping react-props


【解决方案1】:

您需要在代码中使用 return:

import classes from "./Memes.module.css";

const Memes = (props) => {
        
    return (
      < >
      {props.content.map((items)=>{
          return <h1>{items}</h1>
      })}
       </> 
    );
}


export default Memes;

【讨论】:

  • 感谢您的建议!但这不起作用:(
  • @Uttam 我已经编辑了代码,现在检查它会运行。已添加map
  • 嘿,我已经这样做了,但那次没有用。但现在它正在工作..谢谢你的帮助
  • @Uttam 如果此答案对您有帮助,请将其标记为答案。
  • @Uttam 你遇到了什么错误,它对我来说工作得很好。您是否添加了另一段代码?
【解决方案2】:

你可以使用

{props.content.map(items=>{
             return <h4>{items}</h4>
         })}

或者将{}替换为(),react默认返回值:

{props.content.map(items=>(<h4>{items}</h4>))}

更新:试试这个:

import classes from "./Memes.module.css";
import React from "react"
const Memes = ({content})=>{
  return (
    <div>
      {content.map(item=>(<h1>{item}</h1>))}
    </div>
  );
}

export default Memes;

让我知道结果。

【讨论】:

  • 这没用,我之前也试过这个:(
  • 你试过不使用Array.prototype.forEach.call吗?并直接使用props.content.map?另外,你传递给这个元素的道具的名称是什么?
  • 我传递了一个字符串数组 props.content 我从 API 获取了这些字符串数组,是的,我尝试了 props.content.map() 但我不知道为什么它有时会运行但有时会运行不跑突然说props.content.map()不是函数。
  • 它再次发生,当我重新加载我的页面时出现错误。我不知道为什么它有时会运行但有时它不运行并突然说props.content.map()不是函数。
  • 我认为最好也分享你的父组件。
猜你喜欢
  • 2011-09-20
  • 2018-08-10
  • 2017-05-14
  • 1970-01-01
  • 1970-01-01
  • 2017-04-01
  • 2023-03-23
  • 2012-09-21
  • 1970-01-01
相关资源
最近更新 更多