Skip to content

Instantly share code, notes, and snippets.

@jleei
Created October 7, 2019 12:51
Show Gist options
  • Save jleei/43264905857b72d65df4cb90d40296e4 to your computer and use it in GitHub Desktop.
Save jleei/43264905857b72d65df4cb90d40296e4 to your computer and use it in GitHub Desktop.

Revisions

  1. @flexdinesh flexdinesh revised this gist Sep 29, 2019. 1 changed file with 26 additions and 0 deletions.
    26 changes: 26 additions & 0 deletions ProtectedRoute.jsx
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,26 @@
    import React from 'react';
    import PropTypes from 'prop-types';
    import {
    Route,
    Redirect,
    } from 'react-router-dom';

    const ProtectedRoute = ({ component: Component, ...rest }) => {
    const { location: { state: { isAuthenticated = false } = {} } = {} } = rest;
    return (
    <Route
    {...rest}
    render={(props) => (
    isAuthenticated
    ? (<Component {...props} />)
    : (<Redirect to="/login" />)
    )}
    />
    );
    };

    ProtectedRoute.propTypes = {
    component: PropTypes.func
    };

    export default ProtectedRoute;
  2. @flexdinesh flexdinesh revised this gist Aug 16, 2019. 1 changed file with 28 additions and 0 deletions.
    28 changes: 28 additions & 0 deletions lazy-load-retry.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,28 @@
    const MAX_RETRIES = 3;
    const RETRY_INTERVAL = 3000;

    const lazy = (pr: Promise<any>): Promise<any> => {
    const fetchCompomponent = (
    promise: Promise<any>,
    retryCount: number = 0
    ): Promise<any> => {
    return promise.catch(err => {
    retryCount += 1;
    if (retryCount <= MAX_RETRIES) {
    const delayedFetch = new Promise(resolve => {
    const timeoutId = setTimeout(() => {
    clearTimeout(timeoutId);
    resolve(fetchCompomponent(pr, retryCount));
    }, RETRY_INTERVAL);
    });
    return delayedFetch;
    }

    throw err;
    });
    };

    return fetchCompomponent(pr);
    };

    export default lazy;
  3. @flexdinesh flexdinesh revised this gist Jul 16, 2019. 1 changed file with 32 additions and 0 deletions.
    32 changes: 32 additions & 0 deletions AppContext.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,32 @@
    import React, { useState } from 'react';

    const initialState = {
    app: {
    hasLoaded: false
    }
    };

    const AppContext = React.createContext(initialState);

    function AppProvider({ children }) {
    const [app, setAppState] = useState(initialState.app);

    function setAppStateImmutably(appState) {
    const prevState = { ...app };
    setAppState({ ...prevState, ...appState });
    }

    return (
    <AppContext.Provider
    value={{
    app,
    setAppState: setAppStateImmutably
    }}
    >
    {children}
    </AppContext.Provider>
    );
    }

    export default AppProvider;
    export { AppContext };
  4. @flexdinesh flexdinesh revised this gist Mar 29, 2019. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion syntax.js
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    # set state from prev state values
    // set state from prev state values
    this.setState((prevState, props) => ({
    counter: prevState.counter + props.increment
    }));
  5. @flexdinesh flexdinesh renamed this gist Mar 29, 2019. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  6. @flexdinesh flexdinesh created this gist Jan 20, 2019.
    4 changes: 4 additions & 0 deletions syntax
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,4 @@
    # set state from prev state values
    this.setState((prevState, props) => ({
    counter: prevState.counter + props.increment
    }));