-
Notifications
You must be signed in to change notification settings - Fork 106
Add autoEffect #148
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Add autoEffect #148
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
a0025d8
feat(auto-effect): add autoEffect and clearEffect
solkimicreb a270887
docs(auto-effect): document auto effects
solkimicreb d4e6d9f
fix(auto-effect-test): fix a linter related testing issue
solkimicreb 3642c12
fix(auto-effects-test): fix a store placement issue in tests
solkimicreb 9d2e7c5
docs(local-stores): fix a badly formatted link
solkimicreb 8981fef
docs(toc): update the table of contents
solkimicreb File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import React, { Component } from 'react'; | ||
import { render, cleanup } from '@testing-library/react/pure'; | ||
// eslint-disable-next-line import/no-unresolved | ||
import { view, store, autoEffect } from 'react-easy-state'; | ||
|
||
describe('AutoEffect edge cases and errors', () => { | ||
afterEach(cleanup); | ||
|
||
test(`Using autoEffect in a function component ${ | ||
process.env.NOHOOK | ||
? 'with a version of react that has no hooks should' | ||
: 'should not' | ||
} throw an error`, () => { | ||
const someEffect = () => {}; | ||
const person = store({ name: 'Bob' }); | ||
|
||
const MyComp = view(() => { | ||
autoEffect(() => someEffect(person.name)); | ||
return <div>{person.name}</div>; | ||
}); | ||
|
||
if (process.env.NOHOOK) { | ||
expect(() => render(<MyComp />)).toThrow( | ||
'You cannot use autoEffect inside a function component with a pre-hooks version of React. Please update your React version to at least v16.8.0 to use this feature.', | ||
); | ||
} else { | ||
expect(() => render(<MyComp />)).not.toThrow(); | ||
} | ||
}); | ||
|
||
test('Using autoEffect inside a render of a class component should throw an error', () => { | ||
const someEffect = () => {}; | ||
const person = store({ name: 'Bob' }); | ||
|
||
const MyComp = view( | ||
class extends Component { | ||
render() { | ||
autoEffect(() => someEffect(person.name)); | ||
return <div>{person.name}</div>; | ||
} | ||
}, | ||
); | ||
|
||
expect(() => render(<MyComp />)).toThrow( | ||
'You cannot use autoEffect inside a render of a class component. Please use it in the constructor or lifecycle methods instead.', | ||
); | ||
}); | ||
}); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
import React from 'react'; | ||
import { render, cleanup, act } from '@testing-library/react/pure'; | ||
import { | ||
view, | ||
store, | ||
autoEffect, | ||
clearEffect, | ||
// eslint-disable-next-line import/no-unresolved | ||
} from 'react-easy-state'; | ||
|
||
describe('autoEffect', () => { | ||
afterEach(cleanup); | ||
|
||
test('should auto run global effects', () => { | ||
let documentTitle = ''; | ||
const app = store({ name: 'Online Store' }); | ||
|
||
const effect = autoEffect(() => { | ||
documentTitle = app.name; | ||
}); | ||
expect(documentTitle).toBe('Online Store'); | ||
|
||
act(() => { | ||
app.name = 'Learning Platform'; | ||
}); | ||
expect(documentTitle).toBe('Learning Platform'); | ||
|
||
clearEffect(effect); | ||
act(() => { | ||
app.name = 'Social Platform'; | ||
}); | ||
expect(documentTitle).toBe('Learning Platform'); | ||
}); | ||
|
||
test('should auto run local effects in function components', () => { | ||
let documentTitle = ''; | ||
|
||
const app = store({ name: 'Online Store' }); | ||
|
||
const MyComp = view(() => { | ||
autoEffect(() => { | ||
documentTitle = app.name; | ||
}); | ||
return <div>{app.name}</div>; | ||
}); | ||
|
||
const { container, unmount } = render(<MyComp />); | ||
expect(container).toHaveTextContent('Online Store'); | ||
expect(documentTitle).toBe('Online Store'); | ||
|
||
act(() => { | ||
app.name = 'Learning Platform'; | ||
}); | ||
expect(container).toHaveTextContent('Learning Platform'); | ||
expect(documentTitle).toBe('Learning Platform'); | ||
|
||
unmount(); | ||
act(() => { | ||
app.name = 'Social Platform'; | ||
}); | ||
expect(documentTitle).toBe('Learning Platform'); | ||
}); | ||
|
||
test('should be recreated on dependency changes in function components', () => { | ||
let documentTitle = ''; | ||
|
||
const app = store({ name: 'Store' }); | ||
|
||
const MyComp = view(({ name }) => { | ||
autoEffect(() => { | ||
documentTitle = `${name} ${app.name}`; | ||
}, [name]); | ||
return ( | ||
<div> | ||
{name} {app.name} | ||
</div> | ||
); | ||
}); | ||
|
||
const { container, rerender } = render(<MyComp name="Online" />); | ||
expect(container).toHaveTextContent('Online Store'); | ||
expect(documentTitle).toBe('Online Store'); | ||
|
||
rerender(<MyComp name="Awesome" />); | ||
expect(container).toHaveTextContent('Awesome Store'); | ||
expect(documentTitle).toBe('Awesome Store'); | ||
|
||
act(() => { | ||
app.name = 'Page'; | ||
}); | ||
expect(container).toHaveTextContent('Awesome Page'); | ||
expect(documentTitle).toBe('Awesome Page'); | ||
}); | ||
}); |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { useEffect } from 'react'; | ||
import { observe, unobserve } from '@nx-js/observer-util'; | ||
|
||
import { | ||
isInsideFunctionComponent, | ||
isInsideClassComponentRender, | ||
isInsideFunctionComponentWithoutHooks, | ||
} from './view'; | ||
|
||
export function autoEffect(fn, deps = []) { | ||
if (isInsideFunctionComponent) { | ||
return useEffect(() => { | ||
const observer = observe(fn); | ||
return () => unobserve(observer); | ||
}, deps); | ||
} | ||
if (isInsideFunctionComponentWithoutHooks) { | ||
throw new Error( | ||
'You cannot use autoEffect inside a function component with a pre-hooks version of React. Please update your React version to at least v16.8.0 to use this feature.', | ||
); | ||
} | ||
if (isInsideClassComponentRender) { | ||
throw new Error( | ||
'You cannot use autoEffect inside a render of a class component. Please use it in the constructor or lifecycle methods instead.', | ||
); | ||
} | ||
return observe(fn); | ||
} | ||
|
||
export { unobserve as clearEffect }; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,4 @@ | ||
export { observe, unobserve } from '@nx-js/observer-util'; | ||
|
||
export { view } from './view'; | ||
export { store } from './store'; | ||
export { batch } from './scheduler'; | ||
export { autoEffect, clearEffect } from './autoEffect'; |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This was added since the last release and it is not yet released to npm. It won't cause a breaking change.