concept CreatePost in category react

appears as: CreatePost, CreatePost
React in Action

This is an excerpt from Manning's book React in Action.

Listing 5.10. Passing callbacks as props (src/app.js)
import CreatePost from './post/Create'; #1 export default class App extends Component { //... render() { return ( //... <CreatePost onSubmit={this.createNewPost} /> #2 //... ) } //... } #1 Import the component for use. #2 Pass the handlePostSubmit function using props. !@%STYLE%@! {"css":"{\"css\": \"font-weight: bold;\"}","target":"[[{\"line\":0,\"ch\":54},{\"line\":0,\"ch\":56}],[{\"line\":14,\"ch\":0},{\"line\":14,\"ch\":2}],[{\"line\":2,\"ch\":43},{\"line\":2,\"ch\":44}],[{\"line\":4,\"ch\":11},{\"line\":4,\"ch\":12}],[{\"line\":7,\"ch\":26},{\"line\":7,\"ch\":27}],[{\"line\":5,\"ch\":3},{\"line\":5,\"ch\":11}],[{\"line\":7,\"ch\":54},{\"line\":7,\"ch\":56}],[{\"line\":15,\"ch\":0},{\"line\":15,\"ch\":2}]]"} !@%STYLE%@!

6.2.3. Updating CreatePost and adding maps to posts

Now that you’ve created the LocationTypeAhead and DisplayMap components, you can integrate these into the CreatePost component you’ve been working with. This will tie together the functionality you’ve created and allow the user to create posts that have a location. Remember how the CreatePost component passes its data back up to a parent component to do the actual post creation? You’ll do the same thing with the type-ahead and DisplayMap components, but from the CreatePost. They’ll work together but not be so tied to each other that you can’t move them around or use them elsewhere.

Listing 6.11. Handling locations in CreatePost (src/components/post/Create.js)
constructor(props) { super(props); this.initialState = { content: '', valid: false, showLocationPicker: false, #1 location: { #1 lat: 34.1535641, lng: -118.1428115, name: null }, locationSelected: false #1 }; this.state = this.initialState; this.filter = new Filter(); this.handlePostChange = this.handlePostChange.bind(this); this.handleRemoveLocation = this.handleRemoveLocation.bind(this); #2 this.handleSubmit = this.handleSubmit.bind(this); this.handleToggleLocation = this.handleToggleLocation.bind(this); #2 this.onLocationSelect = this.onLocationSelect.bind(this); #2 this.onLocationUpdate = this.onLocationUpdate.bind(this); #2 } //... handleRemoveLocation() { #3 this.setState(() => ({ locationSelected: false, location: this.initialState.location })); } handleSubmit() { if (!this.state.valid) { return; } const newPost = { content: this.state.content }; if (this.state.locationSelected) { #4 newPost.location = this.state.location; } this.props.onSubmit(newPost); this.setState(() => ({ content: '', valid: false, showLocationPicker: false, location: this.initialState.location, locationSelected: false })); } onLocationUpdate(location) { this.setState(() => ({ location })); #5 } onLocationSelect(location) { this.setState(() => ({ #5 location, showLocationPicker: false, locationSelected: true })); } handleToggleLocation(e) { #6 e.preventDefault(); this.setState(state => ({ showLocationPicker: !state.showLocationPicker })); } //... #1 Add keys to state so you can keep track of location and related data; set up some default location data #2 Bind class methods #3 Allow user to remove location from their post #4 When submitting a post, add location to payload if present #5 Handle a location update from the LocationTypeAhead component. #6 Toggle showing the location picker !@%STYLE%@! {"css":"{\"css\": \"font-weight: bold;\"}","target":"[[{\"line\":5,\"ch\":75},{\"line\":5,\"ch\":77}],[{\"line\":6,\"ch\":75},{\"line\":6,\"ch\":77}],[{\"line\":11,\"ch\":75},{\"line\":11,\"ch\":77}],[{\"line\":65,\"ch\":0},{\"line\":65,\"ch\":2}],[{\"line\":5,\"ch\":75},{\"line\":5,\"ch\":77}],[{\"line\":6,\"ch\":75},{\"line\":6,\"ch\":77}],[{\"line\":11,\"ch\":75},{\"line\":11,\"ch\":77}],[{\"line\":65,\"ch\":0},{\"line\":65,\"ch\":2}],[{\"line\":5,\"ch\":75},{\"line\":5,\"ch\":77}],[{\"line\":6,\"ch\":75},{\"line\":6,\"ch\":77}],[{\"line\":11,\"ch\":75},{\"line\":11,\"ch\":77}],[{\"line\":65,\"ch\":0},{\"line\":65,\"ch\":2}],[{\"line\":16,\"ch\":75},{\"line\":16,\"ch\":77}],[{\"line\":18,\"ch\":75},{\"line\":18,\"ch\":77}],[{\"line\":19,\"ch\":75},{\"line\":19,\"ch\":77}],[{\"line\":20,\"ch\":75},{\"line\":20,\"ch\":77}],[{\"line\":66,\"ch\":0},{\"line\":66,\"ch\":2}],[{\"line\":16,\"ch\":75},{\"line\":16,\"ch\":77}],[{\"line\":18,\"ch\":75},{\"line\":18,\"ch\":77}],[{\"line\":19,\"ch\":75},{\"line\":19,\"ch\":77}],[{\"line\":20,\"ch\":75},{\"line\":20,\"ch\":77}],[{\"line\":66,\"ch\":0},{\"line\":66,\"ch\":2}],[{\"line\":16,\"ch\":75},{\"line\":16,\"ch\":77}],[{\"line\":18,\"ch\":75},{\"line\":18,\"ch\":77}],[{\"line\":19,\"ch\":75},{\"line\":19,\"ch\":77}],[{\"line\":20,\"ch\":75},{\"line\":20,\"ch\":77}],[{\"line\":66,\"ch\":0},{\"line\":66,\"ch\":2}],[{\"line\":16,\"ch\":75},{\"line\":16,\"ch\":77}],[{\"line\":18,\"ch\":75},{\"line\":18,\"ch\":77}],[{\"line\":19,\"ch\":75},{\"line\":19,\"ch\":77}],[{\"line\":20,\"ch\":75},{\"line\":20,\"ch\":77}],[{\"line\":66,\"ch\":0},{\"line\":66,\"ch\":2}],[{\"line\":23,\"ch\":75},{\"line\":23,\"ch\":77}],[{\"line\":67,\"ch\":0},{\"line\":67,\"ch\":2}],[{\"line\":36,\"ch\":75},{\"line\":36,\"ch\":77}],[{\"line\":68,\"ch\":0},{\"line\":68,\"ch\":2}],[{\"line\":49,\"ch\":75},{\"line\":49,\"ch\":77}],[{\"line\":52,\"ch\":75},{\"line\":52,\"ch\":77}],[{\"line\":69,\"ch\":0},{\"line\":69,\"ch\":2}],[{\"line\":49,\"ch\":75},{\"line\":49,\"ch\":77}],[{\"line\":52,\"ch\":75},{\"line\":52,\"ch\":77}],[{\"line\":69,\"ch\":0},{\"line\":69,\"ch\":2}],[{\"line\":58,\"ch\":75},{\"line\":58,\"ch\":77}],[{\"line\":70,\"ch\":0},{\"line\":70,\"ch\":2}]]"} !@%STYLE%@!