Skip to content

Commit

Permalink
Allow using for UI management but not API mangement (#51)
Browse files Browse the repository at this point in the history
  • Loading branch information
ChristopherChudzicki committed Apr 13, 2023
1 parent ccd6d6a commit fff19f9
Show file tree
Hide file tree
Showing 7 changed files with 772 additions and 142 deletions.
2 changes: 1 addition & 1 deletion jest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,4 @@ const config: Config.InitialOptions = {
]
}

export default config
export default config
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@mitodl/course-search-utils",
"version": "2.0.4",
"version": "2.1.0",
"description": "JS utils for interacting with MIT Open Course search",
"main": "dist/index.js",
"files": [
Expand Down Expand Up @@ -41,6 +41,7 @@
"devDependencies": {
"@swc/core": "^1.3.0",
"@swc/jest": "^0.2.22",
"@testing-library/react-hooks": "^8.0.1",
"@types/enzyme": "^3.10.7",
"@types/jest": "^29.0.1",
"@types/lodash": "^4.14.162",
Expand Down
15 changes: 9 additions & 6 deletions src/hooks.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
import { useEffect, useRef } from "react"
import { useEffect, useState } from "react"

export function useDidMountEffect(fn: () => void, deps: any[]): void {
const renderedOnce = useRef(false)
/**
* Like `useEffect`, but only runs after component has rendered at least once.
*/
export function useEffectAfterMount(fn: () => void, deps: any[]): void {
const [hasRendered, setHasRendered] = useState(false)

useEffect(() => {
if (renderedOnce.current) {
if (hasRendered) {
fn()
} else {
renderedOnce.current = true
setHasRendered(true)
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, deps)
}, [hasRendered, ...deps])
}
Loading

0 comments on commit fff19f9

Please sign in to comment.