[react-jss] Without a HOC
See original GitHub issueFrom @kof on February 7, 2016 18:10
As an alternative approach to current, we can render sheets as a child element.
const sheet = jss.createStyleSheet(style)
render() {
return (
<div>
<Jss sheet={sheet} />
</div>
)
}
Ref counting can be achieved by using a WeakMap.
const map = new WeakMap()
class Jss {
componentWillMount() {
const {sheet} = this.props
const counter = map.get(sheet) || 0
if (!counter) sheet.attach()
map.set(sheet, counter + 1)
}
componentWillUnmount() {
const {sheet} = this.props
const counter = map.get(sheet) - 1
if (counter) {
map.set(sheet, counter)
} else {
sheet.detach()
map.delete(sheet)
}
}
render() {
return null
}
}
Copied from original issue: cssinjs/react-jss#31
Issue Analytics
- State:
- Created 5 years ago
- Comments:31 (31 by maintainers)
Top Results From Across the Web
Higher-Order Components - React
A higher-order component (HOC) is an advanced technique in React for reusing component logic. HOCs are not part of the React API, per...
Read more >Introduction to Higher-Order Components in React by example
A HOC is a pure function with zero side-effects. Coding a practical Higher-Order Component. Let's say we want to create a list of...
Read more >Flow React HOC typing (without HOC) - Stack Overflow
I ended up with something like this: import { Component as _Component } from 'package' type InjectedProps = {| newProp: boolean, ...
Read more >Higher-Order Components In React - Smashing Magazine
A HOC is a pure function. It has no side effects, returning only a new component. Here are some examples of real-world HOCs...
Read more >React Higher-Order Components (HOCs) - Robin Wieruch
A comprehensive tutorial about Higher-Order Components in React. Higher-Order Components, known as HOCs, are often a difficult to understand ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
From @kof on July 13, 2017 18:6
Another idea:
I think we should close this one in favor of #907, wdyt?