Skip to content

Instantly share code, notes, and snippets.

// /src/store/hooks/QuoteHook.js
import { maybe } from '@7urtle/lambda';
import types from "../types";
import { requestQuote, getQuoteFromNetlify } from '../effects/QuoteEffect';
/**
* getQuote is a hook that processes requesting a quote through QuoteEffect asynchronously.
*
// /src/store/effects/NetlifyFunction.js
import { AsyncEffect, isEqual, Maybe, map } from '@7urtle/lambda';
import axios from 'axios';
const api_url = isEqual('development')(process.env.NODE_ENV) ? 'http://localhost:5000' : '/.netlify/functions';
/**
* Ansynchronous post request to a Netlify Function on an input path
* that maybe returns a returns response data based on input data.
// src/store/effects/QuoteEffect.js
import { AsyncEffect, Maybe, randomOf, keysOf } from '@7urtle/lambda';
import quotes from '../../../quotesList';
/**
* Finds a random quote in an array of quotes and returns it.
*
* @HindleyMilner getRandomQuote :: [[a]] -> [a]
return ({
...state, // add everything from the previous state
quote: action.payload.quote, // change quote based on payload
author: action.payload.author // change author based on payload
}); // return new state
// src/store/reducers.js
import types from "./types";
const initialState = {
quote: 'Creativity is intelligence having fun.',
author: 'Albert Einstein'
};
const reducer = (state = initialState, action) => {
// /src/store/actions.js
import types from "./types";
export const useActions = (state, dispatch) => ({
requestRandomQuote: payload => dispatch({type: types.REQUEST_RANDOM_QUOTE, payload: payload})
});
// /src/store/types.js
const types = {
REQUEST_RANDOM_QUOTE: 'REQUEST_RANDOM_QUOTE',
RECEIVE_RANDOM_QUOTE: 'RECEIVE_RANDOM_QUOTE'
};
export default types;
import React, { useState, useEffect } from 'react';
import axios from 'axios';
const MyComponent = () => {
const [numberOfUsers, setNumberOfUsers] = useState(0);
useEffect(() => {
axios.get('/users/count')
.then(response => setNumberOfUsers(response.data.count))
.catch(error => console.log('that is an error my friends: ' + error));
}, []);
import React, { useContext } from 'react';
import { StoreContext } from '../store/StoreContext';
const MyComponent = () => {
const { state, actions } = useContext(StoreContext);
const myAction = () => actions.triggerAction('data');
return(
<>
Number of button clicks: {state.counter}.
const expression = input => input.toLowerCase();
const expression2 = function(input) {
return input.toLowerCase();
};
const statement = () => console.log('hello world');
const statement2 = function() {
console.log('hello world');
};