marcosC
December 20, 2024, 2:55pm
1
Hello!
Was doing React and Redux - Manage State Locally First and got stuck in something before figuring out what was wrong.
But the thing is that I don’t know why it’s wrong.
To sum up, we need to create a React Component with Controlled Input; got this:
class DisplayMessages extends React.Component {
constructor(props) {
super(props);
this.state = {
input: '',
messages: []
};
this.handleChange = this.handleChange.bind(this);
};
handleChange(event) {
// #1 - CALLING CALLBACK
this.setState(state => ({
input: event.target.value
}));
console.log(this.state);
// #2 - NOT CALLING CALLBACK
this.setState({
input: event.target.value
});
console.log(this.state);
};
render() {
return (
<div>
<h2>Type in a new Message:</h2>
<input onChange={this.handleChange} value={this.state.input}/>
<button onClick={this.submitMessage}>Submit</button>
</div>
);
}
};
So, in the handleChange(event) {}:
I tried calling a callback with state as parameter.
// #1 - CALLING CALLBACK
this.setState(state => ({
input: event.target.value
}));
console.log(this.state);
But it throws error.
The problem doesn’t happen when I change event.target.value, inside the callback, for a value.
So, my question is:
Is it really the problem? Calling event.target.value inside the callback for the setState() method?
If it is, why is that?
Also, the problem doesn’t happen when I don’t use the callback.
Could you give some more info, what is for and how are you running it, in other words what do I need to do to check the code?
1 Like
marcosC:
event.target.value
Hi there!
That is not defined with this
keyword.
1 Like
marcosC
December 20, 2024, 5:45pm
4
Hi!
You can copy and past the whole code in FCC editor, erasing everything is previously there:
can be in this lesson
Then, inside handleChange(event), try commenting one of the bellow and try to write something in the rendered input.
// #1 - CALLING CALLBACK
this.setState(state => ({
input: event.target.value
}));
console.log(this.state);
// #2 - NOT CALLING CALLBACK
this.setState({
input: event.target.value
});
console.log(this.state);
The first one throws an error. The second doesn’t.
I thought that the first one would work as well, but it doesn’t.
But if I change event.target.value
inside the this.setState(state => {})
for a string or number than it works just fine.
So I wondering if there is a problem in using event.target.value
. And if there is, why would that be.
1 Like
marcosC
December 20, 2024, 5:55pm
5
Hello!
So, I tried the following:
// #1 - CALLING CALLBACK
this.setState(state => ({
input: this.event.target.value
}));
console.log(this.state);
It didn’t work as well.
First, not sure if that is what you suggested - if it isn’t please, could you point the correct place to define this
?
Second, this
isn’t to reference object or method to the object itself?
Event, in this case, would become part of it?
Am I getting event
and this
concepts wrong?
lasjorg
December 20, 2024, 6:44pm
6
Please include the challenge link when asking for help.
As for your question
You are not using the current state for the state update. This means the updater function is not needed and you should just be using an object.
handleChange(event) {
this.setState({
input: event.target.value
})
}
Next. The fCC React curriculum is using React v16.4.0 and because of how React 16 events work (pooling ) the event target will be undefined inside the setState updater function.
This is why it crashes when you type into the input Uncaught TypeError: Cannot read propert…
2 Likes
Teller
December 20, 2024, 6:56pm
7
Hi @marcosC
Here is a comparison of the original code and your code.
The code in blue is the original code, the code in red is your code.
The code in magenta is the overlap.
You need to include the submitMessage()
method.
Please reset the step, as you removed some of the comments.
I think the tests check for those.
Happy coding
1 Like
lasjorg
December 20, 2024, 7:02pm
8
You are right of course that they didn’t complete the challenge. But I think they are specifically asking why the event doesn’t work inside the state updater function, which is for technical reasons explained in the thread I linked to.
1 Like
marcosC
December 20, 2024, 8:43pm
9
Yeah!
That’s exactly the answer to my question!
I could solve this challenge, just didn’t know why one thing worked and not the other.
So it really is something that caused problem before.
Thanks a lot, including the links to the docs and other sources.
Will follow the advice of not using updater function when there is no need of previous data.
Also the warning about including challenge link.
Appreciate the help of everybody that took their time to help!
1 Like