OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

Passing a Component to other component through a property in an array of objects

  • Thread starter Thread starter Step
  • Start date Start date
S

Step

Guest
I have an array like this:

Code:
const actions = [
    {
      header: "Money",
      type: "icon",
      icon: <Icon.DollarSign
        key={5}
          style={{
            display:  "block" ,
        }}
        size={20}
        color="#88CFE0"
        onClick={() => console.log('Dollar')}
        className="cursor-pointer"
      />
      ,
      visible: true,
    },
  ]

I'm trying to render that component inside another component in this way (I'm passing the "actions" object as a prop):

Code:
export const ActionsPersonalice = ({
  actions,
  action,
  data,
  validateDataPoa,
}) => {
    return (
        <>
          {actions.map((item, index) => {
            if (item.type == "button") {
              return (
                <button
                  key={index}
                  style={{
                    fontWeight: "400",
                    color: "#88CFE0",
                    textDecoration: "underline",
                    border: "none",
                    backgroundColor: "transparent",
                    display: item.visible ? "block" : "none",
                  }}
                  className="cursor-pointer"
                  type="button"
                >
                  {item.header}
                </button>
              );
            }
            if (item.type == "icon") {
               return item.icon
            }
          })}

But when I tried this, an error is raised:

Objects are not valid as a React child (found: [missing argument]). If you meant to render a collection of children, use an array instead.

If I pass the icon inside an property directly (not inside the array), the component is rendered correctly.

Anyone knows how I can fix that behaviour when I use an array?

Thanks in advance.

<p>I have an array like this:</p>
<pre><code>const actions = [
{
header: "Money",
type: "icon",
icon: <Icon.DollarSign
key={5}
style={{
display: "block" ,
}}
size={20}
color="#88CFE0"
onClick={() => console.log('Dollar')}
className="cursor-pointer"
/>
,
visible: true,
},
]
</code></pre>
<p>I'm trying to render that component inside another component in this way (I'm passing the "actions" object as a prop):</p>
<pre><code>export const ActionsPersonalice = ({
actions,
action,
data,
validateDataPoa,
}) => {
return (
<>
{actions.map((item, index) => {
if (item.type == "button") {
return (
<button
key={index}
style={{
fontWeight: "400",
color: "#88CFE0",
textDecoration: "underline",
border: "none",
backgroundColor: "transparent",
display: item.visible ? "block" : "none",
}}
className="cursor-pointer"
type="button"
>
{item.header}
</button>
);
}
if (item.type == "icon") {
return item.icon
}
})}
</code></pre>
<p>But when I tried this, an error is raised:</p>
<blockquote>
<p>Objects are not valid as a React child (found: [missing argument]). If
you meant to render a collection of children, use an array instead.</p>
</blockquote>
<p>If I pass the icon inside an property directly (not inside the array), the component is rendered correctly.</p>
<p>Anyone knows how I can fix that behaviour when I use an array?</p>
<p>Thanks in advance.</p>
 

Latest posts

M
Replies
0
Views
1
MOHAMED AMIIN ABDI AADAN
M
Top