I was following this tutorial this tutorial where he uses this kind of Component declaration which I find peculiar (for a newbie in React)
const Story = ({ story, columns, onArchive }) => {
const {
title,
url,
author,
comments,
points,
objectID,
} = story;
return (
<div className="story">
<span style={{ width: columns.title.width }}><a href={url}>{title}</a></span>
<span style={{ width: columns.author.width }}>{author}</span>
<span style={{ width: columns.comments.width }}>{comments}</span>
<span style={{ width: columns.points.width }}>{points}</span>
<span style={{ width: columns.archive.width }}>
<ButtonInline onClick={() => onArchive(objectID)}>Archive</ButtonInline>
</span>
</div>
);
}
Basically he has the component in a function that doesn't have a render function -- just straight up return.
Out of curiosity, I recreated it using the more familiar approach:
class Story extends React.Component {
render() {
const {
title,
url,
author,
comments,
points,
objectID,
} = this.props.story;
return(
<div className="story">
<span style={{ width: this.props.columns.title.width }}><a href={url}>{title}</a></span>
<span style={{ width: this.props.columns.author.width }}>{author}</span>
<span style={{ width: this.props.columns.comments.width }}>{comments}</span>
<span style={{ width: this.props.columns.points.width }}>{points}</span>
<span style={{ width: this.props.columns.archive.width }}>
<ButtonInline onClick={() => this.props.onArchive(objectID)}>Archive</ButtonInline>
</span>
</div>);
}
}
and it renders the same.
Now, I wonder the following:
Does the former implementation accept states? If so, how would one code the constructor and the state declaration?
Is it more efficient than the latter? It certainly looks more concise and straight to the point, but what are the pros and cons of this coding style?
What is it called? I'd love to read more about this.
Thanks!