question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[react-jss] Without a HOC

See original GitHub issue

From @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:closed
  • Created 5 years ago
  • Comments:31 (31 by maintainers)

github_iconTop GitHub Comments

2reactions
HenriBeckcommented, Aug 3, 2018

From @kof on July 13, 2017 18:6

Another idea:


const styles = {
  button: {
    color: 'red'
  }
}

const StyledButton = () => (
      <Jss styles={styles}>
        {({classes}) => (
          <button className={classes.button}>My Button</button>
        )}
      </Jss>
)
1reaction
kofcommented, Dec 10, 2018

I think we should close this one in favor of #907, wdyt?

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found