Can we use props in function components in React, and if then, how?I was trying to do
console.log(this.props)
above the return function in a function component but it kept giving me errors.
Can we use props in function components in React, and if then, how?I was trying to do
console.log(this.props)
above the return function in a function component but it kept giving me errors.
Yes. You pass the props inside the function component directly (using props
) like this:
function myFunc(props){
console.log(props.content);
return <div>{props.content}</div>;
}
assuming that the props
that passes in has element content
.
To clarify, there is no this
in function components because they're not classes at all! Instead, you pass in props
directly into the function.
In fact, it is probably the one of the first things that you learn in React. So if you're new, then I suggest the tutorial here.
Yes, like this,
the props are passed to the function component as the first argument
export default function Welcome(props) {
return <h1>Hello, {props.name}</h1>;
}
here read this article it explains everything: https://medium.com/@PhilipAndrews/react-how-to-access-props-in-a-functional-component-6bd4200b9e0b
You can access "this" keyword in class component ... In functional component you can access the props just by calling it as below.
import React from 'react'
const Printing=(props)=> {
console.log(props)
return (
<div>
</div>
)
}
export default Printing