How to convert component with many states to react functions

0 votes
With the help of code and example can u tell me How to convert component with many states to react functions?
Feb 21 in Node-js by Nidhi
• 11,580 points
56 views

1 answer to this question.

0 votes

When converting a class component with multiple states to a functional component, the best approach is to replace this.state with multiple useState hooks.

Example:

import React, { useState } from "react";

const UserProfile = () => {

  const [name, setName] = useState("Alice");

  const [age, setAge] = useState(25);

  const [email, setEmail] = useState("alice@example.com");

  const updateProfile = () => {

    setName("Bob");

    setAge(30);

  };

  return (

    <div>

      <h2>Name: {name}</h2>

      <p>Age: {age}</p>

      <p>Email: {email}</p>

      <button onClick={updateProfile}>Update Profile</button>

    </div>

  );

};

export default UserProfile;

answered Feb 22 by Kavya

Related Questions In Node-js

0 votes
1 answer

How do you model a many-to-many relationship in MongoDB with an example?

In MongoDB, a many-to-many relationship can be ...READ MORE

answered Feb 23 in Node-js by Kavya
67 views
0 votes
1 answer

How to pass parameters with react-router?

Passing Parameters with React Router 1. Define a ...READ MORE

answered Feb 23 in Node-js by Kavya
90 views
0 votes
0 answers

How to build a real-time chat app with react node Socket.IO and HarperDB?

Can someone help me with the code ...READ MORE

3 days ago in Node-js by Nidhi
• 11,580 points
18 views
0 votes
1 answer

Error:Parse Error: Adjacent JSX elements must be wrapped in an enclosing tag

Hello @kartik, It is happening because any where ...READ MORE

answered Jun 4, 2020 in Angular by Niroj
• 82,840 points
2,649 views
0 votes
1 answer

Error:setState doesn't update the state immediately

Hello @kartik, The method setState() takes a callback. And ...READ MORE

answered Jun 4, 2020 in Angular by Niroj
• 82,840 points
5,229 views
0 votes
1 answer

From php returning JSON to JavaScript

Hii @kartik, You can use Simple JSON for PHP. ...READ MORE

answered Jun 5, 2020 in Java-Script by Niroj
• 82,840 points
1,163 views
0 votes
1 answer
0 votes
1 answer

How can I use all the React events with Material-UI components?

The best approach is to leverage the ...READ MORE

answered Feb 22 in Node-js by Kavya
41 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP