0

Update:

Basically i want the same output but i restructured the content. I'm not sure if your answers are still up for that.

Please check my sandbox feel free to fork it here: https://codesandbox.io/s/get-the-property-value-forked-hutww

So on ContentData.js i want my image alt tag to be dynamic and pull the content of it from key name it something like this alt={this.name} and it will generate to alt="My alt tags"

See the codes below:

Content.js

import React, { Component } from "react";
import mainListsItems from "./ContentData";

class Content extends Component {
  render() {
    const myContent = mainListsItems.map((lists, k) => (
      <>
        <div key={k}>{lists.text}</div>
        {lists.mainContent.map((subcontent, j) => {
          return <div key={j}>{subcontent.contentAll}</div>;
        })}
      </>
    ));

    return <>{myContent}</>;
  }
}

export default Content;

ContentData.js

import React from "react";

const listsData = [
  {
    id: 1,
    name: "My alt tags",
    text: (
      <>
        <p>Lorem Imsum</p>
      </>
    ),
    mainContent: [
      {
        contentAll: (
          <>
            <p>
              Lorem Ipsum is simply dummy text of the printing and typesetting
              industry.
            </p>
            <img
              alt=""
              src="https://images.unsplash.com/photo-1637704758245-ed126909d374?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxlZGl0b3JpYWwtZmVlZHwxNHx8fGVufDB8fHx8&auto=format&fit=crop&w=500&q=60"
            />
          </>
        )
      }
    ]
  }
];

export default listsData;
clarkf
  • 547
  • 2
  • 10
  • 22
  • Please add the error to the question. – adiga Nov 24 '21 at 10:30
  • 1
    Not directly answering your question, but have you considered setting the relevant `src` as the value itself? You could then replace the `

    {x.content}

    ` with `{x.name}`
    – tomleb Nov 24 '21 at 10:32
  • I think you should take a look at [this answer to a similar problem](https://stackoverflow.com/a/12789163/9135987). – jackthedev Nov 24 '21 at 10:33
  • The problem is that you are trying to reference the same object during its initialization (using the `this` keyword), which is not allowed (on line 12). I would consider populating the `alt` in the same place where `name` is populated, reducing complexity on your frontend – Bruno Farias Nov 24 '21 at 10:36
  • It's better to put on the array only the src (so only the link instead of all the img tag) inside `content` and then take it from the `output` function where you can write the `` tag – Giacomo Nov 24 '21 at 10:37
  • I just updated the comment and codes above, please check. – clarkf Nov 25 '21 at 09:29

3 Answers3

2

content is defined inside the object while the object is being defined. So there is no name yet when content is being defined. Only after the assignment does name exist. You're referencing something that doesn't exist yet. So instead you can create a function that will be called at a later point after the object is defined and then the name can be referenced as shown below.

export default function App() {
const myData = [
    {
      id: 1,
      name: "Lorem Ipsum",
      content: function(){
        return (
        <>
          <img
            src="https://images.unsplash.com/photo-1637704758245-ed126909d374?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxlZGl0b3JpYWwtZmVlZHwxNHx8fGVufDB8fHx8&auto=format&fit=crop&w=500&q=60"
            alt={this.name}
          />
        </>
      )
        }
    }
  ];

 const output = myData.map((x) => (
         <>
           <div key={x.id}>
             <p>{x.name} sa</p>
             <p>{x.content()}</p>
           </div>
         </>
  ));

  return <div className="App">{output}</div>;
}
Prince Mittal
  • 326
  • 3
  • 15
1

The this, in your case is referring the window object.

You can try to pass the name as a function value. Something like this :

const myData = [
    {
      id: 1,
      name: "Lorem Ipsum",
      content: (alt) => (
        <>
          <img
            src="https://images.unsplash.com/photo-1637704758245-ed126909d374?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxlZGl0b3JpYWwtZmVlZHwxNHx8fGVufDB8fHx8&auto=format&fit=crop&w=500&q=60"
            alt={alt}
          />
        </>
      )
    }
  ];

const output = myData.map((x) => (
    <div key={x.id}>
      <p>{x.name} sa</p>
      <p>{x.content(x.name)}</p>
    </div>
  ));
Dharman
  • 30,962
  • 25
  • 85
  • 135
or-yam
  • 41
  • 1
  • 7
1

You can replace the object in your myData array with a self executing function like this:

 const myData = [
     function(){
        const entry = {
           id: 1,
           name: "Lorem Ipsum",
        }
        return {
          ...entry,
          content: (
             <>
               <img
                 src="your image source"
                 alt={entry.name}
               />
             </>
           )
        }
     }()
  ];
jackthedev
  • 578
  • 5
  • 7