Created
August 3, 2017 20:21
-
-
Save kocisov/4520b9545cd9f5f198d02137d3ac34fe to your computer and use it in GitHub Desktop.
tp02l
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// example | |
// don't overthink this too much | |
class X extends PureComponent { | |
// constructor equivalent for creating initial state | |
state = { | |
something: '' | |
} | |
// function that returns another function | |
changeReturn = () => e => { | |
this.setState({ | |
[e.target.name]: e.target.value | |
}) | |
} | |
// function that sets controlled input value | |
change = e => { | |
this.setState({ | |
[e.target.name]: e.target.value | |
}) | |
} | |
// render | |
render() { | |
return ( | |
<div> | |
Immediately called | |
<input type="text" name="something" placeholder="Write something..." onChange={this.changeReturn()}> | |
Not immediately called | |
<input type="text" name="something" placeholder="Write something..." onChange={this.change}> | |
</div> | |
) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment