Last active
November 5, 2017 17:14
-
-
Save m-torin/90331b579b79fd6aef294af38fce6edb to your computer and use it in GitHub Desktop.
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> | |
) | |
} | |
} |
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 { observer } from 'mobx-react' | |
import { func, bool } from 'prop-types' | |
// Separate local imports from dependencies | |
import './styles/Form.css' | |
// Declare propTypes here, before the component (taking advantage of JS function hoisting) | |
// You want these to be as visible as possible | |
ExpandableForm.propTypes = { | |
onSubmit: func.isRequired, | |
expanded: bool, | |
onExpand: func.isRequired | |
} | |
// Destructure props like so, and use default arguments as a way of setting defaultProps | |
function ExpandableForm({ onExpand, expanded = false, children, onSubmit }) { | |
const formStyle = expanded ? { height: 'auto' } : { height: 0 } | |
return ( | |
<form style={formStyle} onSubmit={onSubmit}> | |
{children} | |
<button onClick={onExpand}>Expand</button> | |
</form> | |
) | |
} | |
// Wrap the component instead of decorating it | |
export default observer(ExpandableForm) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment