What are props? How to pass and read them? React JS interview question
Arguments passed to components can be accessed using this this.props object. They look similar to regular HTML element attributes. We use this.props object to read parameters that were passed to the component.
We pass parameters to the component:
<Comment name="My Name"/>
In the component we use this.props object to read from it:
class Comment extends React.Component {
render (){
return (
<p className="comment">{this.props.name}</p>
)
}
}
We pass parameters to the component:
<Comment name="My Name"/>
In the component we use this.props object to read from it:
class Comment extends React.Component {
render (){
return (
<p className="comment">{this.props.name}</p>
)
}
}
Comments