Skip to content

Instantly share code, notes, and snippets.

View StevenJL's full-sized avatar

Steven Li StevenJL

  • Fountain Inc.
  • SF Bay Area
  • 10:52 (UTC -07:00)
View GitHub Profile
class CompleteOrder
def perform
notify_recipient(order.recipient)
update_inventory(order.item_number)
prepare_shippping(order.shipping_address)
end
private
def order
class User extends React.PureComponent {
// ...
updateUserStatus(status) {
const { user } = this.state
this.state.user.status = status
}
class User extends React.Component {
// ...
updateUserStatus(status) {
const { user } = this.state;
setState({ user: {...user, status: status }})
}
appendItem(newItem) {
import shallowEqual from 'shared/shallowEqual';
/*
shallowEqual is a helper function from the React source code:
https://github.com/facebook/react/blob/72434a7686035b4af766ee7d06c070d7f5d6a5f2/packages/shared/shallowEqual.js
*/
shouldComponentUpdate(nextProps, nextState) {
return !shallowEqual(this.props, nextProps) || !shallowEqual(this.State, nextState);
}
import PropTypes from 'prop-types';
Widget.propTypes = {
extraArray: PropTypes.array.isRequired,
userNames: PropTyes.arrayOf(PropTypes.string).isRequired,
userPosts: PropTypes.arrayOf(PropTypes.shape({
id: PropTypes.number.isRequired,
title: PropTypes.string.isRequired,
body: PropTypes.string.isRequired,
postDate: PropTypes.instanceOf(Date).isRequired
import PropTypes from 'prop-types';
Widget.propTypes = {
// Specify that prop is an object
extraData: PropTypes.object.isRequired,
// Specify that prop is an object with certain keys
userData: PropTypes.shape({
name: PropTypes.string,
age: PropTypes.number
import PropTypes from 'prop-types'
Widget.propTypes = {
headerText: PropTypes.string.isRequired,
widgetCount: PropTypes.number.isRequired,
isFetchingData: PropTypes.bool.isRequired,
handlerFunction: PropTypes.func.isRequired,
createdAt: PropTypes.instanceOf(Date).isRequired,
optionalText: PropTypes.string
}
import PropTypes from 'prop-types';
class ItemComponent extends React.Component {
// propTypes can be defined inside the Component definition
static propTypes = {
name: PropTypes.string.isRequired,
price: PropTypes.number.isRequired,
purchaseHandler: PropTypes.func.isRequired
};
import PropTypes from 'prop-types';
const ItemDiv = ({ name, price, purchaseHandler }) => {
return(
<div>
div> Item:{name} </div>
div> Price:${price} </div>
<button onClick={purchaseHandler}> Purchase </button>
</div>
)
class ControlledComponent extends React.Component {
constructor( props) {
super(props)
this.handleSubmit = this.handleSubmit.bind(this)
}
handleSubmit(event) {
event.preventDefault()
this.props.createAccount(