Skip to content

Instantly share code, notes, and snippets.

useEffect(() => {
axios.get(url)
.then(response => changeTheDataSomehow())
.catch(error => console.log('hups');
},
[url]);
// src/components/MyComponent.js
import React, {useContext} from 'react';
import {StoreContext} from '../store/StoreContext';
const MyComponent = () => {
const {state, actions} = useContext(StoreContext);
return(
<button onClick={() => actions.triggerAction('data')}>Click me</button>
useMemo(() => {
actions.fetchDataAction();
}, []);
// src/store/actions.js
import {types} from '.reducers';
export const useActions = (state, dispatch) => ({
triggerAction: data => dispatch({ type: types.TRIGGER_ACTION, payload: data })
});
// src/store/reducers.js
import match from 'conditional-expression';
const initialState = {
something: 'data',
somethingElse: 'also data'
};
const types = {
// src/store/middleware.js
import match from 'conditional-expression';
import axios from 'axios';
import {types} from './reducers';
export const applyMiddleware = dispatch => action =>
dispatch(action) ||
match(action.type)
.equals(types.TRIGGER_ACTION).then(() => {
// src/store/StoreContext.js
import React, { createContext, useReducer } from 'react';
import { reducer, initialState } from './reducers';
import { useActions } from './actions';
import { applyMiddleware } from './middleware';
const StoreContext = createContext(initialState);
const StoreProvider = ({ children }) => {
// index.js
import React from 'react';
import { render } from 'react-dom';
import { StoreProvider } from './store/StoreContext';
import App from './App';
render(
<StoreProvider>
<App />
</StoreProvider>,
import {Animal, Dog} from './animals';
import {Robot} from './robots';
class RoboDog {
constructor(animal, dog, robot) {
this.animal = new animal();
this.dog = new dog();
this.robot = new robot();
}
move() {
// monadic (unary) function accepts only one argument
const monadic = one => one + 1;
// this is not monadic (unary)
const notMonadic = (one, two) => one + two;
// this is curry, monadic and higher-order function
const curry = one => two => one + two;