-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use theme context when rendering components at all times (#2424)
* unconditionally use useContext * add changeset * Update .changeset/light-swans-guess.md: major -> patch Co-authored-by: Mateusz Burzyński <mateuszburzynski@gmail.com> * update global.js with fix * tests to ensure serializeStyles is always invoked with theme * adjust tests * Update .changeset/light-swans-guess.md Co-authored-by: Mateusz Burzyński <mateuszburzynski@gmail.com>
- Loading branch information
Showing
5 changed files
with
92 additions
and
7 deletions.
There are no files selected for viewing
This file contains 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,6 @@ | ||
--- | ||
'@emotion/react': patch | ||
--- | ||
|
||
|
||
Use theme context when rendering components at all times. This removes a conditional usage of a React hook that could break [Rules of Hooks](https://reactjs.org/docs/hooks-rules.html) in some scenarios. |
This file contains 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,46 @@ | ||
// @flow | ||
/** @jsx jsx */ | ||
import 'test-utils/dev-mode' | ||
import { render } from 'react-dom' | ||
import { jsx, css, CacheProvider, ThemeProvider } from '@emotion/react' | ||
import createCache from '@emotion/cache' | ||
|
||
// $FlowFixMe | ||
console.error = jest.fn() | ||
|
||
beforeEach(() => { | ||
// $FlowFixMe | ||
document.head.innerHTML = '' | ||
// $FlowFixMe | ||
document.body.innerHTML = `<div id="root"></div>` | ||
|
||
jest.clearAllMocks() | ||
}) | ||
|
||
describe('EmotionElement', () => { | ||
test('no React hook order violations', () => { | ||
const theme = { color: 'blue' } | ||
const cache = createCache({ key: 'context' }) | ||
|
||
// $FlowFixMe | ||
const Comp = ({ flag }) => ( | ||
<ThemeProvider theme={theme}> | ||
<CacheProvider value={cache}> | ||
<div | ||
css={ | ||
flag && | ||
(t => css` | ||
color: ${t.color}; | ||
`) | ||
} | ||
/> | ||
</CacheProvider> | ||
</ThemeProvider> | ||
) | ||
|
||
render(<Comp />, document.getElementById('root')) | ||
expect(console.error).not.toHaveBeenCalled() | ||
render(<Comp flag />, document.getElementById('root')) | ||
expect(console.error).not.toHaveBeenCalled() | ||
}) | ||
}) |
This file contains 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 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 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