问题尝试将属性传递给子组件

huangapple go评论55阅读模式
英文:

Problem trying to pass props to child component

问题

I'm making my first project with React and everything is working fine but one simple thing... but I don't know what I'm missing and I think I tried everything.

I'm trying to pass some props/info to a child component like this but it seems it's not receiving any info. What am I missing here? That console.log in the child component is throwing an empty object whatever I try to do.

In my main App.jsx I have this code:

<Popup miData={{title:'Hello world', text:'Lorem ipsum...'}}/>

and that child component is something like:

import React from "react"
const Popup = (miData) => {
   console.log(miData)
    return (
        html code here is fine, component is being rendered correctly
    )
}
export default Popup

(both components are being rendered fine, everything works fine but this passing props thing)

英文:

I´m making my first project with React and everything is working fine but one simple thing... but I don´t know what I´m missing and I think I tried everything.

I´m trying to pass some props/info to a child component like this but it seems it´s not receiving any info. What am I missing here? That console.log in the child component is throwing an empty object whatever I try to do.

In my main App.jsx I have this code:

&lt;Popup miData={{title:&#39;Hello world&#39;, text:&#39;Lorem ipsum...&#39;}}/&gt;

and that child component is something like:

import React from &quot;react&quot;
const Popup = (miData) =&gt; {
   console.log(miData)
    return (
        html code here is fine, component is being rendered correctly
    )
}
export default Popup

(both components are being rendered fine, everything works fine but this passing props thing)

答案1

得分: 1

尝试在你的返回语句中添加React片段。

import React from "react";
const Popup = (miData) => {
   console.log(miData);
    return (
      <>
        这里的HTML代码没问题组件被正确渲染了
      </>
    );
}
export default Popup;

在我的沙盒上运行正常。

英文:

try adding react Fragments in your return statement

import React from &quot;react&quot;
const Popup = (miData) =&gt; {
   console.log(miData);
    return (
      &lt;&gt;
        html code here is fine, component is being rendered correctly
      &lt;/&gt;
    );
}
export default Popup;

Its working for me on my sandbox

huangapple
  • 本文由 发表于 2023年4月17日 18:15:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/76034045.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定