0

I've created function which returns me drawed photo. I'm trying to assign it as an img url, but instead of html element it returns me an object. How can I refer to an html element instead of to object?

This is function that returns me an image:

function workoutImage() {
  const images = [
    {
      img1: "./images/workout-image1.jpg",
    },
    {
      img2: "./images/workout-image2.jpg",
    },
    {
      img3: "./images/workout-image3.jpg",
    },
  ];

  const imageIndex = Math.floor(Math.random() * images.length);
  let returnImage = images[imageIndex];

  return returnImage;
}

export default workoutImage;

and here's component where I'm trying to display it:

function WorkoutsDiary() {

  let workoutImage = workoutImageDraw();

  return (
    <Container>
      <Content>
        {showWorkouts &&
          showWorkouts.map((workout, key) => (
            <Image key={key}>
              <Link to={`/exercise/` + workout.id}>
                <img src={workoutImage} alt="" />
              </Link>
            </Image>
          ))}
      </Content>
    </Container>
  );
}

when I do a console log, it looks like this:

enter image description here

5 Answers 5

2

This:

return returnImage;

is returning an instance of an object. For example, this:

{
  img1: "./images/workout-image1.jpg",
}

If you just want it to return the string property on that object, return that property:

return returnImage.img1;

Alternatively, if you want to return the whole object then just use its string property in the attribute value:

<img src={workoutImage.img1} alt="" />

Edit: I just noticed that your objects all have different property names so you'd need to determine which one to use. Instead, I would recommend having a single common object structure. Or forget the objects entirely and just use an array of strings:

const images = [
  "./images/workout-image1.jpg",
  "./images/workout-image2.jpg",
  "./images/workout-image3.jpg",
];
Sign up to request clarification or add additional context in comments.

Comments

2

You have an array of objects, and you have different keys in each of the objects.

It should be array of strings.

const images = [
  "./images/workout-image1.jpg",
  "./images/workout-image2.jpg",
  "./images/workout-image3.jpg"
];

Comments

1

The easiest thing to do is modify your workoutImage in this way:

function workoutImage() {
  const images = [
    "./images/workout-image1.jpg",
    "./images/workout-image2.jpg",
    "./images/workout-image3.jpg",
  ];

  const imageIndex = Math.floor(Math.random() * images.length);
  let returnImage = images[imageIndex];

  return returnImage;
}

export default workoutImage;

Now you should see your image in img.

Comments

0
import img from workoutImage

and use image like this

 <img src={img} alt="" />

Comments

0

Change this line :

let returnImage = images[imageIndex];

To :

    let returnImage = '';
Object.keys(images[imageIndex]).map((key) => {
  returnImage = images[imageIndex][key]
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.