0

I created an component

<div>
<img src= {this.props.source} style={this.props.style} />
</div>

And in other component I call this component to create my images with:

render() {
return (
<Image source={src_image} style={imgstyle}/>
<Image source={src_image} style={imgstyle}/>
<Image source={src_image} style={imgstyle}/>
<Image source={src_image} style={imgstyle}/>
)}

My images are created as I want.Now I want to loop all my images to modify all their style by function button .Please How to loop all my images one by one in the function button ?

2 Answers 2

1

Create an array of <Image/> components in your render() function, iterate through that array:

render() {
  let images = [];
  for (let i = 0; i < 4; i++) { // Use whatever looping you need here
    images.push(<Image source={src_image} key={i} style={imgstyle}/>); // Edit your images here
  }
  return (
    <div>{images}</div>
  );
}

In the example above, we are using the index as the key but if your array is not static, you need a unique identifier as your key instead as the index is not reliable when the array can change. For instance, if your images are unique, you can use the image source (or a hash of it) as your key. More about lists and keys.

Sign up to request clarification or add additional context in comments.

3 Comments

I want to modify them by function button onclick. I must just add this code to my function button ?
@John No, the function should modify a variable in your component's state, which you should then use in the loop in render()
For example, you might have an array of image sources, then you can iterate through in the loop - <Image source={this.state.sources[i]}...> etc
0

If you initialize an Images array, you can use :

  render() {
      const { images } = this.props;
      // images = [{src_image: '...', imgstyle: {...}}, {src_image: '...', imgstyle: {...}}]

      return images.map((image) => <Image source={image.src} style={image.imgstyle}/>)
    }

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.