Skip to content

Instantly share code, notes, and snippets.

@scottdomes
Last active February 15, 2018 13:49
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save scottdomes/19d4ade69b1a3978730b104558fb5308 to your computer and use it in GitHub Desktop.
Save scottdomes/19d4ade69b1a3978730b104558fb5308 to your computer and use it in GitHub Desktop.
React Component Best Practices: Functional Component
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)
@richie-south
Copy link

Shouldn't it be onSubmit Here L18 instead of onExpand? 😃

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment