简体   繁体   中英

What is the correct way to pass props to component in React?

I want to know which is a better method or the "correct way" to pass props to a component, in object or plain props:

plain props:

{ this.props.data && this.props.data.map((item) => {
  return(
    <MyComponent title={ item.title } thumbnail={ item.thumbnail } />
  )
});
}

or object:

{ this.props.data && this.props.data.map((item) => {
  return(
    <MyComponent singleItem = { item } />
  )
});
}

Which I should use?

Either should work. However, if you plan on using shouldComponentUpdate to minimize re-rendering and either of title or thumbnail are JS primitives then you should pass them separately to take advantage of shallow equality.

If you want to pass them the props separately but still want to not have to write the props separately, you can use the spread notation:

 <MyComponent {...item} /> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM