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

Exercise 03: Why must 'name' be explicitly used? (Spoilers) #71

Open
qwelyt opened this issue Jul 25, 2017 · 1 comment
Open

Exercise 03: Why must 'name' be explicitly used? (Spoilers) #71

qwelyt opened this issue Jul 25, 2017 · 1 comment

Comments

@qwelyt
Copy link

qwelyt commented Jul 25, 2017

Exercise 03, task 1.

In the onNameChange(event) function you take an event and is supposed to use that event to update the state.

The working solutions, for the tests, is to explicitly name the key, ie: this.setState({name: event.target.value});

But, if you want to be more general, and you don't want to explicitly say that it's the "name" attribute that should always be updated, you can do this:

onNameChange(event){
   let obj = {};
   obj[event.target.name] = event.target.value;
   this.setState(obj);
}

Which works great in the browser, but fails in the tests.

Is there a specific reason for this that I'm missing?

@glenwinters
Copy link

The tests are simulating the events, and target.name doesn't exist in the simulated event.

Here's an example from the tests:

React.addons.TestUtils.Simulate.change(input, {target: { value: "XYZ" } });

Your code works if you change it to:

React.addons.TestUtils.Simulate.change(input, {target: { name: "name", value: "XYZ" } });

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

No branches or pull requests

2 participants