Skip to content

Instantly share code, notes, and snippets.

View takahirohonda's full-sized avatar

Takahiro Honda takahirohonda

View GitHub Profile
@takahirohonda
takahirohonda / refactoring-react-es6-class-components-with-property-initialisers-1.js
Created July 23, 2019 12:18
refactoring-react-es6-class-components-with-property-initialisers-1
class Form extends ReactComponents {
constructor(props) {
super(props)
this.state = {id: ''}
this.submitHandler = this.submitHandler.bind(this)
this.nameInputHandler = this.nameInputHandler.bind(this)
this.idInputHandler = this.idInputHandler.bind(this)
}
@takahirohonda
takahirohonda / refactoring-react-es6-class-components-with-property-initialisers-2.js
Created July 23, 2019 12:20
refactoring-react-es6-class-components-with-property-initialisers-2
class Form extends ReactComponents {
state = {id: ''}
submitHandler = () => {
// do more things here ....
// then dispatch action
this.props.onSubmitHandler()
}
@takahirohonda
takahirohonda / refactoring-react-es6-class-components-with-property-initialisers-3.js
Created July 23, 2019 12:21
refactoring-react-es6-class-components-with-property-initialisers-3
const Form = ({state, onInputHandler, onSubmitHandler}) => {
return (
<form>
<label for="firstname">First name</label>
<input
type="text" name="firstname" id="firstname"
onChange={e => onInputHandler(e.target.name, e.target.value)}
/>
<label for="id">Id</label>
<input
@takahirohonda
takahirohonda / calling-async-function-in-action-with-redux-thunk-1.js
Created July 24, 2019 07:45
calling-async-function-in-action-with-redux-thunk-1
export const getPersonalData = (payload) => (dispatch) => {
// Make AJAX call
return postRequest(payload)
.then((response) => {
// Once AJAX call is successful, update the store
dispatch(setPersonalData(response))
})
.catch(err => dispatch(apiCallNotSuccess()))
}
@takahirohonda
takahirohonda / calling-async-function-in-action-with-redux-thunk-2.js
Created July 24, 2019 07:46
calling-async-function-in-action-with-redux-thunk-2
function dispatch(action) {
if (!isPlainObject(action)) {
throw new Error(
'Actions must be plain objects. ' +
'Use custom middleware for async actions.'
)
}
...
@takahirohonda
takahirohonda / calling-async-function-in-action-with-redux-thunk-3.js
Created July 24, 2019 07:48
calling-async-function-in-action-with-redux-thunk-3
function createThunkMiddleware(extraArgument) {
return ({ dispatch, getState }) => next => action => {
if (typeof action === 'function') {
return action(dispatch, getState, extraArgument);
}
return next(action);
};
}
@takahirohonda
takahirohonda / calling-async-function-in-action-with-redux-thunk-4.js
Created July 24, 2019 07:49
calling-async-function-in-action-with-redux-thunk-4
import React from 'react'
import ReactDom from 'react-dom'
import App from './App'
import '../public/css/bootstrap.css'
import '../public/css/custom.css'
import { Provider } from 'react-redux'
import { createStore, compose, applyMiddleware } from 'redux'
import reducer from './reducers'
import thunk from 'redux-thunk'
@takahirohonda
takahirohonda / calling-async-function-in-action-with-redux-thunk-5.js
Created July 24, 2019 07:51
calling-async-function-in-action-with-redux-thunk-5
export const setPersonalData = (personalData) => {
return {
type: UPDATE_PesonalData,
personalData
}
}
export const getPersonalData = (payload) => (dispatch) => {
// Update the store on the status of API call (optional)
dispatch(apiCallInProgress())
@takahirohonda
takahirohonda / setting-up-unit-tests-for-react-with-mocha-jsdom-and-enzyme-1.js
Last active July 24, 2019 10:27
setting-up-unit-tests-for-react-with-mocha-jsdom-and-enzyme-1
import React from 'react'
import expect from 'expect'
import { shallow, mount } from 'enzyme'
import FormInput from '../form-elements/FormInput'
describe('Form Component', () => {
it('should returns correct label for firstname', () => {
const wrapper = mount(<FormInput
inputType = "firstname"
@takahirohonda
takahirohonda / setting-up-unit-tests-for-react-with-mocha-jsdom-and-enzyme-2.js
Created July 24, 2019 10:24
setting-up-unit-tests-for-react-with-mocha-jsdom-and-enzyme
import React from 'react'
import { expect } from 'chai'
import { shallow, mount } from 'enzyme'
import sinon from 'sinon'
import Form from '../Form'
import initialState from '../../reducers/initialState'
describe('<Form />', () => {
let state;