I'm trying to make the following code prettier. Specifically, I would like to pass the values to the map function using another function.
Here is the code:
...
this.props.slides.map((s)=>{
let id = s.get('id')
let title = s.get('title')
let image = s.get('image')
let alt = s.get('alt')
let caption = s.get('caption')
return(
<Carousel.Item key={id} >
<img width={900} height={500} alt={s.get('alt')} src={image} alt={alt}/>
<Carousel.Caption>
<h3>{title}</h3>
<p>{caption}</p>
</Carousel.Caption>
</Carousel.Item>
)
})
}
</Carousel>)
}
}
...
So basically I need another method there (it's all part of a class) like:
listSlides(){
...
}
and then I can just pass that to my map function. But, I'm unsure on how to write it. I'm a bit confused about the way map() works.
The docs state that the first parameter is a function that's being passed to each item in the array but I'm unsure why does that mean that I can now do: let id = s.something in myclosured function?
map()into a function, and put the function name inmap(listSlides). If you run into a problem post the code you tried.listSlidesshould not really a become a method of your class, it does not do anything special. It should be just a function declaration (inside the method or outside the class), a static method maybe, but better a method ofsif at all.