What is the minimum definition of a Component ?
What is the minimum definition of a Component ?
The minimum definition of a React component is a JavaScript function or class that can accept an arbitrary input (known as props
, short for "properties") and returns a React element that describes what should appear on the screen. In its simplest form, a functional component in React can be written as:
function MyComponent(props) {
return <div>Hello, {props.name}!</div>;
}
This function is a valid React component because it accepts a single "props" (which stands for properties) object argument with data and returns a React element. Here, MyComponent
is a functional component that takes props
as an argument and returns a simple JSX element that renders a div
with the text "Hello," followed by the value of props.name
.
Alternatively, a React component can also be defined as a class that extends React.Component
:
class MyComponent extends React.Component {
render() {
return <div>Hello, {this.props.name}!</div>;
}
}
In this class component, MyComponent
extends React.Component
and implements a render
method that returns a React element, which is...
junior
Gợi ý câu hỏi phỏng vấn
Chưa có bình luận nào