Creating Functional Components in React
10 Questions
0 Views

Creating Functional Components in React

Created by
@PromptMossAgate6024

Questions and Answers

How do you create a functional component in React?

  • class MyComponent extends React.Component
  • const MyComponent = () => { return <div>Hello</div>; }
  • function MyComponent() { return <div>Hello</div>; } (correct)
  • React.createComponent(MyComponent)
  • What is the purpose of useState in React?

  • It handles side effects in components
  • It adds state to a functional component (correct)
  • It renders components conditionally
  • It passes data between components
  • How do you pass data from a parent component to a child component?

  • Using useState
  • Via props, e.g., <ChildComponent propName={value} /> (correct)
  • Using context API
  • By mutating the child component's state directly
  • How can you handle an event in React?

    <p>By passing a function to the event attribute, e.g., &lt;button onClick={handleClick}&gt;Click</button></p> Signup and view all the answers

    What is JSX?

    <p>A syntax extension that looks similar to HTML, used in React to describe UI structure</p> Signup and view all the answers

    How do you conditionally render a component in React?

    <p>Using a ternary operator or logical &amp;&amp;, e.g., {condition ? &lt;Component /&gt; : null}</p> Signup and view all the answers

    What is the purpose of useEffect in React?

    <p>It handles side effects in functional components, like fetching data or updating the DOM</p> Signup and view all the answers

    How do you create a React component with props?

    <p>function MyComponent(props) { return <div>{props.name}</div>; }</p> Signup and view all the answers

    How do you handle forms in React?

    <p>By controlling form inputs using useState and handling submit events</p> Signup and view all the answers

    How do you import a component in React?

    <p>import MyComponent from './MyComponent'</p> Signup and view all the answers

    More Quizzes Like This

    Use Quizgecko on...
    Browser
    Browser