Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Alternative approach #7

Closed
dagolinuxoid opened this issue Aug 26, 2019 · 1 comment
Closed

Alternative approach #7

dagolinuxoid opened this issue Aug 26, 2019 · 1 comment

Comments

@dagolinuxoid
Copy link

In my App.js I have the method to handle adding a new user (with name and job fields)

    addCharacter = (name, job) => {
      this.setState(
        {
          characters: [...this.state.characters, {name, job}]
        }
      );
    }

And I use the functional Form component :

const Form = (props) => {
  return (
    <form>
      <input name='name' />
      <input name='job' />
      <button type='button' onClick={() => {
        props.addCharacter(
          // use any preferred approach to reference the value in need
          document.forms[0].elements.name.value,
          document.forms[0].elements.job.value
        )
        }}>
        Add
      </button>
    </form>
  )
}

Since your source code is drastically different ( it has a bit more lines and it's kind of hard to easily comprehend for me yet), can you point disadvantages of the way I approached it ( if any ) ?

@taniarascia
Copy link
Owner

You should never be referencing document in React. You should always use state and event. Your way of retrieving values may be easier for you to understand right now, but the way outlined in the article is how you will see it done in almost any other React app.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants