6

I am learning React and in the process I am developing my JavaScript skills which are currently beginner level. I have a component that represents a registration form and the form's input values (username, email password etc) are stored in the component's state.

The form's state is the following:

this.state = {
    username: "",
    password: "",
    email: "",
    firstName: "",
    lastName: "",
    country: "",
    region: "",
    phoneNumber: "",
    selectCountries: []
};

Let's stay I want to create a new object to pass to a registration method, which would only include the username, email and password fields. What is the best way and fastest way to do so?

Currently what I do is manually map the properties I want like so:

const { username, email, password } = this.state;

let registrationData = {
    username, email, password
};

Is this the best way to do it? Is there any way to merge these two statements into one?

Any input would be greatly appreciated, thanks a lot!

Codehack
  • 323
  • 2
  • 13

0 Answers0