1

I am trying to learn ReactJS and I'm having some trouble understanding the destructuring occurring in the following snippet:

const IngredientsList = ({ list }) =>  
    React.createElement('ul', null,
      list.map((ingredient, i) =>
        React.createElement('li', {key: i}, ingredient))

const Ingredients = React.createFactory(IngredientsList)
const list = [
     "1 lb Salmon",
     "1 cup Pine Nuts",
     "2 cups Butter Lettuce",
 ]    

This should be equivalent to:

 const IngredientsList = props =>
  ....
     props.list.map(...)

I thought only on an object such a destructuring is available. Can you shed some light how the two of above are equivalent? Is it something specific to react?

1
  • 1
    props is an object. list is an array passed down to the component in props. Commented Aug 11, 2017 at 23:12

1 Answer 1

1

Let's format your example and add one extra line to show how to use it:

const IngredientsList = ({list}) => {
  return React.createElement("ul", null, list.map((ingredient, i) => {
    return React.createElement("li", {key: i}, ingredient)
  }))
}

const Ingredients = React.createFactory(IngredientsList)
const list = [
  "1 lb Salmon",
  "1 cup Pine Nuts",
  "2 cups Butter Lettuce",
]

// usage:
Ingredients({list})

As you can see, what you pass to Ingredients is the props Object. The same object can be destructured as the argument of the IngedientsList function.

You can find in this explanation another example of using React.createFactory, again: passing an object which contains the props.

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

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.