Last active
January 13, 2021 09:36
-
-
Save kirandash/7aa08f87bfcfd67779c743ef3d976778 to your computer and use it in GitHub Desktop.
React Tips
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
// React: how to update state.item[1] in state using setState? | |
this.setState(({items}) => ({ | |
items: [ | |
...items.slice(0,1), | |
{ | |
...items[1], | |
name: 'newName', | |
}, | |
...items.slice(2) | |
] | |
})); | |
// Removing element from array in component state | |
removeItem(index) { | |
this.setState({ | |
data: this.state.data.filter((_, i) => i !== index) | |
}); | |
} | |
// React - Implementing maxlength of 6 for input type number | |
<input type="number" onInput={(e) => { | |
e.target.value = Math.max(0, parseInt(e.target.value, 10)).toString().slice(0, 6)// maxLength 6 for number | |
}} /> | |
// React - setState | |
handleChange = (event) => { | |
const { target: { name, value } } = event | |
this.setState({ [name]: value }) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment