Last active
September 28, 2018 18:02
-
-
Save scottdomes/70e436951dfeeb82dfdd4aa6d2541dce to your computer and use it in GitHub Desktop.
React Component Best Practices: Class Component
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, { Component } from 'react' | |
import { observer } from 'mobx-react' | |
import { string, object } from 'prop-types' | |
// Separate local imports from dependencies | |
import ExpandableForm from './ExpandableForm' | |
import './styles/ProfileContainer.css' | |
// Use decorators if needed | |
@observer | |
export default class ProfileContainer extends Component { | |
state = { expanded: false } | |
// Initialize state here (ES7) or in a constructor method (ES6) | |
// Declare propTypes as static properties as early as possible | |
static propTypes = { | |
model: object.isRequired, | |
title: string | |
} | |
// Default props below propTypes | |
static defaultProps = { | |
model: { | |
id: 0 | |
}, | |
title: 'Your Name' | |
} | |
// Use fat arrow functions for methods to preserve context (this will thus be the component instance) | |
handleSubmit = (e) => { | |
e.preventDefault() | |
this.props.model.save() | |
} | |
handleNameChange = (e) => { | |
this.props.model.name = e.target.value | |
} | |
handleExpand = (e) => { | |
e.preventDefault() | |
this.setState(prevState => ({ expanded: !prevState.expanded })) | |
} | |
render() { | |
// Destructure props for readability | |
const { | |
model, | |
title | |
} = this.props | |
return ( | |
<ExpandableForm | |
onSubmit={this.handleSubmit} | |
expanded={this.state.expanded} | |
onExpand={this.handleExpand}> | |
// Newline props if there are more than two | |
<div> | |
<h1>{title}</h1> | |
<input | |
type="text" | |
value={model.name} | |
// onChange={(e) => { model.name = e.target.value }} | |
// Avoid creating new closures in the render method- use methods like below | |
onChange={this.handleNameChange} | |
placeholder="Your Name"/> | |
</div> | |
</ExpandableForm> | |
) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Very interesting!