Created
October 7, 2019 12:51
-
-
Save jleei/43264905857b72d65df4cb90d40296e4 to your computer and use it in GitHub Desktop.
React Snippets #react
This file contains hidden or 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 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 }; |
This file contains hidden or 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
| 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; |
This file contains hidden or 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 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; |
This file contains hidden or 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
| // set state from prev state values | |
| this.setState((prevState, props) => ({ | |
| counter: prevState.counter + props.increment | |
| })); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment