Peter Zacharias Peter Zacharias - 7 months ago 41
React JSX Question

What does this render method do: const {images, selectedImage} = this.state;?

I'm doing a simple tutorial on react/ redux and I do not understand why we have to, in the render() method, do

const {images, selectedImage} = this.state;

I understand that we need to assign the previously defined variables to the constructor, so it becomes part of our component state and is not just a variable floating around in our component only, but I do no get why we cannot just access them directly in the render method later? I also do not understand why they are defined as constants? I also don't get why they are assigned to state and why they have parentheses aroudn them? Thanks a lot for your help

import React, {Component} from 'react'

const flickrImages = [

export default class Gallery extends Component {
constructor(props) {
this.state = {
images: flickrImages,
selectedImage: flickrImages[0]

handleThumbClick(selectedImage) {

render() {
const {images, selectedImage} = this.state; // why do we have to do this? And why does this need to be a constant?
return (
<div className="image-gallery">
<div className="gallery-image">
<img src={selectedImage} />
<div className="image-scroller">
{, index) => (
<div key={index} onClick={this.handleThumbClick.bind(this,image)}>
<img src={image}/>

PS: if you can suggest a better title for this question please let me know, i was not sure what to call it, and I'm sure there's a better name the sort of thing that's being done here


It's called object destructuring.

It assigns this.state.images to the local constant images and this.state.selectedImage to the local constant selectedImage.