This file contains 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
// Import at the top: | |
import update from 'immutability-helper'; | |
updateState({target}) { | |
let user = update(this.state.user, {$merge: {[target.name]: target.value}}); | |
this.setState({user}); | |
} |
This file contains 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
updateState(event) { | |
const {name, value} = event.target; | |
let user = {...this.state.user, [name]: value}; | |
this.setState({user}); | |
} |
This file contains 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
updateState({target}) { | |
this.setState({user: {...this.state.user, [target.name]: target.value}}); | |
} |
This file contains 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
// At top, import immutable | |
import { Map } from 'immutable'; | |
// Later, in constructor... | |
this.state = { | |
// Create an immutable map in state using immutable.js | |
user: Map({ firstName: 'Cory', lastName: 'House'}) | |
}; | |
updateState({target}) { |
This file contains 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
updateState({target}) { | |
this.setState({user: {...this.state.user, [target.name]: target.value}}); | |
doSomething(this.state.user) // Uh oh, setState merely schedules a state change, so this.state.user may still have old value | |
} |
This file contains 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
updateState({target}) { | |
this.setState((prevState) => { | |
const updatedUser = {...prevState.user, [target.name]: target.value}; // use previous value in state to build new state... | |
return { user: updatedUser }; // And what I return here will be set as the new state | |
}, () => this.doSomething(this.state.user); // Now I can safely utilize the new state I've created to call other funcs... | |
); | |
} |
This file contains 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
import React from 'react'; | |
class App extends React.Component { | |
constructor() { | |
this.state = { | |
users: [ | |
{ id: 1, name: 'Cory' }, | |
{ id: 2, name: 'Meg' } | |
] | |
}; |
This file contains 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
import React from 'react'; | |
import PropTypes from 'prop-types'; | |
class UserListItem extends React.Component { | |
onDeleteClick = () => { | |
// No bind needed since we can compose | |
// the relevant data for this item here | |
this.props.onClick(this.props.user.id); | |
} |
This file contains 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
import React from 'react'; | |
import UserListItem from './UserListItem'; | |
class App extends React.Component { | |
constructor(props) { | |
super(props); | |
this.state = { | |
users: [ | |
{ id: 1, name: 'Cory' }, | |
{ id: 2, name: 'Meg' } |
This file contains 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
import React from 'react'; | |
import { render } from 'react-dom'; | |
import UserListItem from './UserListItem'; | |
class App extends React.Component { | |
constructor(props) { | |
super(props); | |
this.state = { | |
users: [{ id: 1, name: 'Cory' }, { id: 2, name: 'Sherry' }], | |
}; |