Closed easoncxz closed 4 years ago
After getting some help from a friend, I have made the code work via the following changes:
--- before.js 2020-08-22 02:16:34.000000000 +1200
+++ after.js 2020-08-22 02:16:25.000000000 +1200
@@ -24,7 +24,7 @@
<button onClick={this.lodgeNewTask.bind(this)}>
Add #{this.state.items.length + 1}
</button>
- <button onClick={this.clearInboxBox}>
+ <button onClick={this.clearInputBox}>
Clear input
</button>
</form>
@@ -32,8 +32,9 @@
);
}
clearInputBox(e) {
- alert('clearing input bo x, ok???');
- this.setState(() => { text: '' });
+ this.setState((state) => {
+ return Object.assign({}, state, { text: '' });
+ });
}
Doh!!
Turns out I made two mistakes:
Component.prototype.setState
method.The typo was what took me forever to find; I think without the second pair of eyes (or a type-checker!), I would spend hours before finding this bug.
As for setState
: from what I gathered with some fiddling around, it seems to support two APIs:
Partial<StateType>
, like this: this.setState({ text: '' })
(s: StateType) => StateType
, like this: this.setState(s => ({ ...s, text: '' }))
I mixed the two, and passed a lambda, but a lambda that returned a patch (type (s: StateType) => Partial<StateType>
). Nope, that ain't gonna work!
So this issue is resolved now.
Notably, I'm trying to get the "Clear input" button to make the input box empty.
Expected behaviour:
Actual behaviour:
I'm working with this code on the React homepage, so it's perhaps most convenient to copy my code and paste it into the left-hand side code-box half of section "An Application" on https://reactjs.org/.