m567
September 28, 2018, 1:38pm
1
Tell us what’s happening:
Heii, I don’t know how the event.target.value works…
Can someone explain me?
(I know my code is wrong)
I know that “event” is the argument, but what are “target” and “value” ?
Your code so far
class ControlledInput extends React.Component {
constructor(props) {
super(props);
this.state = {
input:''
};
// change code below this line
this.handleChange = this.handleChange.bind(this)
// change code above this line
}
// change code below this line
handleChange(event) {
this.setState({event.target.value})
}
// change code above this line
render() {
return (
<div>
{ /* change code below this line */}
<input input={this.state.input} onChange={handleChange}/>
{ /* change code above this line */}
<h4>Controlled Input:</h4>
<p>{this.state.input}</p>
</div>
);
}
};
Your browser information:
User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36
.
Link to the challenge:
https://learn.freecodecamp.org/front-end-libraries/react/create-a-controlled-input
1 Like
Basically it retrieves value of whatever input it was called on.
In this case, it’s your input element so whatever you insert in your input can be accessed through event.target.value
3 Likes
m567
September 28, 2018, 1:45pm
3
But how can I use it?
So, how can I write it in my code?
Like you are using it here.
You can set a value in your component’s state to it.
m567
September 28, 2018, 1:49pm
5
So I using it correctly?
Because the program say, it is wrong:
(
SyntaxError: unknown: Unexpected token, expected , (14:23)
12 |
13 | handleChange(event) {
14 | this.setState({event.target.value})
| ^
15 | }
16 | // change code above this line
17 | render() {
)
Update: The Arrow shows the dot between “event” and “target”
you are not specifying which key
in your state object you want to update
in this case you want to update the input
key in your state
1 Like
This is correct
m567:
But the “input” should be determined by the Users Input.
You have defined input in your state with:
this.state = {
input:''
};
This is the part where the users input should update the input key in your state.
You are getting the value from the input textbox but it is not updating the input
key in your state
handleChange(event) { this.setState({event.target.value}) }
The way event
works, it is all objects. It is applicable to all javascript, not only in react.
event
provides a set of properties
that can be accessed. Where you’ll find that set of properties is in their instruction reference called DOM API .
You access objects’ properties by using dot .
notation
example:
var dog = {
legs: 4,
name: 'doggie'
}
console.log(dog.name) // -> doggie
Same way how event.target
works
event.target.value
is called chaining because you combine the two processes.
var target = event.target;
var value = target.value;
So when finding which properties to access, you can start from this search term at google “DOM API html input
”. You will see this result: https://developer.mozilla.org/en-US/docs/Web/API/HTMLInputElement
And there you’ll see what properties you can access. You will see value
there and other bunch of properties.
6 Likes
Other people have explained it well, go head and go back and read them again.
Think of how you assign values to keys in an object literal which was covered in previous exercises.
m567
October 1, 2018, 7:00am
13
Thank you, know I understand it!
Nice job. Remember to mark as solved
the post when it is already done.
handleChange = (event) => {
this.setState({input: event.target.value})
}
Hey, this article should be useful!
Events are actions or occurrences that happen in the system you are programming, which the system tells you about so you can respond to them in some way if desired. For example, if the user clicks a button on a webpage, you might want to respond to...
1 Like
Hi
And how do I get the value from a selecti input??
event.target.value return: unified