staged-components
Make React function component staged.
Last updated 3 years ago by awmleer .
MIT · Repository · Bugs · Original npm
$ cnpm install staged-components 
SYNC missed versions from official npm registry.

Staged Components

Make React function component staged.

Install

yarn add staged-components
# or
npm install --save staged-components

Usages

React Hook is awesome, but it has some kind of rules. One of these rules is "Only Call Hooks at the Top Level".

So the component below will cause an error:

const App = function(props) {
  if (props.user === undefined) return null
  const [name, setName] = useState(props.user.name)
  // React Hook "useState" is called conditionally. React Hooks must be called in the exact same order in every component render. Did you accidentally call a React Hook after an early return?
  return (
    <input value={name} onChange={(e) => {setName(e.target.value)}}/>
  )
}

With staged-components, you can safely "break" this rule:

const App = staged((props) => { // stage 1
  if (props.user === undefined) return null
  return () => { // stage 2
    const [name, setName] = useState(props.user.name)
    return (
      <input value={name} onChange={(e) => {setName(e.target.value)}}/>
    )
  }
})

Advanced

Usage with forwardRef:

const App = forwardRef(staged((props, ref) => {
  if (props.user === undefined) return null
  return () => {
    useImperativeHandle(ref, () => 'hello')
    return (
      <h1>{props.user.name}</h1>
    )
  }
}))

Current Tags

  • 1.1.3                                ...           latest (3 years ago)

2 Versions

  • 1.1.3                                ...           3 years ago
  • 1.1.2                                ...           4 years ago
Maintainers (1)
Downloads
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 0
Dependencies (0)
None
Dev Dependencies (12)
Dependents (0)
None

Copyright 2013 - present © cnpmjs.org