This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
useEffect(() => { | |
axios.get(url) | |
.then(response => changeTheDataSomehow()) | |
.catch(error => console.log('hups'); | |
}, | |
[url]); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
useMemo(() => { | |
actions.fetchDataAction(); | |
}, []); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// src/store/actions.js | |
import {types} from '.reducers'; | |
export const useActions = (state, dispatch) => ({ | |
triggerAction: data => dispatch({ type: types.TRIGGER_ACTION, payload: data }) | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// src/store/reducers.js | |
import match from 'conditional-expression'; | |
const initialState = { | |
something: 'data', | |
somethingElse: 'also data' | |
}; | |
const types = { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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(() => { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 }) => { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// index.js | |
import React from 'react'; | |
import { render } from 'react-dom'; | |
import { StoreProvider } from './store/StoreContext'; | |
import App from './App'; | |
render( | |
<StoreProvider> | |
<App /> | |
</StoreProvider>, |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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; |