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 { useRef, useEffect, useCallback } from "react"; | |
// Rate limit a function but make sure the last call in a series of calls is run. | |
function useRateLimitedCallback(timeout, callback) { | |
// Save the callback in a ref to allow using a un-memoized callback parameter without changing identity of the returned callback function | |
const callbackRef = useRef(callback); | |
useEffect(() => { | |
callbackRef.current = callback; | |
}); |
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 injectFunc = WrappedComponent => | |
class InjectFunc extends React.Component { | |
render() { | |
return <WrappedComponent func={this.log} {...this.props}/>; | |
} | |
log = () => console.log("This function is from the HOC!"); | |
} |
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
@injectText | |
class MyPage extends React.Component { | |
render() { | |
console.log(this.props.text); // Will write "This text is from the HOC!" to console. | |
return <SomeContent/>; | |
} | |
} |
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 injectText = WrappedComponent => | |
class InjectText extends React.Component { | |
return <WrappedComponent text="This text is from the HOC!" {...this.props}/>; | |
} |
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
@doNothing | |
class MyPage extends React.Component { | |
render() { | |
return <SomeContent/>; | |
} | |
} |
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 doNothing = WrappedComponent => | |
class DoNothing extends React.Component { | |
render() { | |
return <WrappedComponent {...this.props}/> | |
} | |
}; | |
export default doNothing; |
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
class App extends React.Component { | |
render() { | |
return ( | |
<div> | |
<StudentPage/> | |
<OtherPage/> | |
<AndOtherPage}/> | |
<div> | |
); | |
} |
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
class App extends React.Component { | |
render() { | |
return ( | |
<div> | |
<DataContainer component={StudentPage} fetchData={() => fetch("myschool/student")}/> | |
<DataContainer component={OtherPage} fetchData={() => fetch("myschool/other")}/> | |
<DataContainer component={AndOtherPage} fetchData={() => fetch(("myschool/andanother")}/> | |
<div> | |
); | |
} |
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
class App extends React.Component { | |
render() { | |
return ( | |
<div> | |
<StudentContainer/> | |
<OtherContainer/> | |
<AndAnotherContainer/> | |
<div> | |
); | |
} |