Skip to content

Instantly share code, notes, and snippets.

View StevenJL's full-sized avatar

Steven Li StevenJL

  • Fountain Inc.
  • SF Bay Area
  • 13:23 (UTC -07:00)
View GitHub Profile
class ControlledComponent extends React.Component {
constructor( props) {
super(props)
this.state = {
name: 'Firstname Lastname',
email: '[email protected]',
password: ''
}
this.handleChange = this.handleChange.bind(this)
class ButtonComponent extends React.Component {
constructor(props) {
super(props)
this.state = {
message: 'Invoked from inside an anonymous function!',
}
}
handleClick() {
console.log(this.state.message)
// Pure component with no argument
const OrangeDiv = () => <div style={orangeDivStyle}></div>
// Pure component with one argument (props)
const TextDiv = (props) => <div><p>{ props.textBody }</p></div>
// Pure component with two arguments (props and context)
const TextDivWithContext = (props, context) => (
<button>{ context.language }{ props.value} </button>
)
class ButtonComponent extends React.Component {
constructor(props) {
super(props)
this.state = {
message: 'I need to be manually binded!',
}
this.handleClick = this.handleClick.bind(this)
}
const ButtonComponent = React.createClass({
getInitialState() {
return { message: "Auto-binded!" }
},
handleClick() {
console.log(this.state.message)
},
render() {
import { thunk } from redux-thunk;
const store = createStore(
reducer,
applyMiddleware(thunk.withExtraArgument(api))
)
// In the actions
fetchUser = (id) => {
return (dispatch, getState, api) => {
awesome_users = User.where(payment_status: "on_time")
# Instead of updating each individual record
deadbeat_users.each do |user|
user.update(awesome: true)
end
# Just do one bulk update
User.where(payment_status: "on_time").update_all(awesome: true)
/*
* Valid redux middleware signature
* @param {object} store, the redux store
* @param {function} next, function that continues flow of execution
* @param {object} action, the originally dispatched action
*/
validMiddleware = (store) => (next) => (action) => {
// apply middleware logic
next(action)
// action creators
// Action Creator returns action
loadUser = (userData) => ({
type: "LOAD_USER_DATA", userData
})
/*
Action Creator which returns function which dispatches other actions
*/
function createThunkMiddleware(extraArgument) {
return ({ dispatch, getState }) => next => action => {
if (typeof action === 'function') {
return action(dispatch, getState, extraArgument);
}
return next(action);
};
}