React Question & Answer

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • MyrinNew
    Senior Member
    • Feb 2024
    • 5175

    #1

    React Question & Answer

    1)What is the difference between a functional component and a class component in React?


    Functional Components:
    • Functional component JavaScript function that returns JSX UI elements.
    • Easy to read, and with React Hooks, it can manage state and side effects


    Example:






    `function Greeting (props){
    return(
    Hello ,{props.name}

    ;
    )

    }`







    Class Component:
    • Class component is a JavaScript class that extends React.Component
    • Has access to lifecycle methods (componentDidMount, componentDidUpdate, etc.)
    • Before Hooks, class components were the only way to use state and lifecycle logic.
    • Class components take more code and are less simple than functional components.


    Example:






    `class Greeting extends React.Component {
    render() {
    return Hello, {this.props.name}!

    ;
    }
    }`









    More...
Working...