What is the best way to publish a generalized preact/react component?
See original GitHub issueI have a simple react link component
At the top it has:
var React = require('react');
But it does not bundle react, instead it defines it as a peerDependency.
Installing that module with core preact (without compat) causes a ‘react not found’ error, because browserify, aliasify and friends don’t act on node_modules.
What is the best way to publish a component that is compatible with core preact and react?
Issue Analytics
- State:
- Created 7 years ago
- Comments:15 (12 by maintainers)
Top Results From Across the Web
What is the best way to publish a generalized preact/react ...
Another option, possibly the preferable one, would be to export a factory that produces your component. Eric Elliot promotes this approach as a ......
Read more >Switching to Preact (from React)
1. Install Preact · 2. JSX Pragma: transpile to `h()`. Via Babel; Via Comments; Via Bublé · 3. Update any Legacy Code ·...
Read more >Don't Optimize Your React App, Use Preact Instead | by Nilanth
PREACT is a 3KB library preconfigured with performance optimization, includes Progressive Web Apps (PWA) and alternative to ReactJS.
Read more >The Better Way to Type React Components | by Mikael Brevik
Using props directly on parameters allow you to more correctly type components and avoid false positives while also being more flexible.
Read more >How We Reduced Our React App's Load Time by 60% - InfoQ
Use shouldComponentUpdate to let React know that the component's result is not affected by the state change to control when a component should ......
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
There are a couple of options, depending on what formats you need to support. If you’re just doing commonjs, you can try this:
Another option, possibly the preferable one, would be to export a factory that produces your component. Eric Elliot promotes this approach as a form of Dependency Injection.
The usage would then be:
Lastly, most people are pretty used to aliasing
react
out forpreact
in their bundler of choice (browserify, webpack, etc). If they have aliasedReact:preact
, all you’d have to do is make sure to normalizeh()
vscreateElement()
:One thing to note: preact (without
preact-compat
, which most people alias) doesn’t supportcreateClass()
, onlyComponent
. Currently yourLink
component doesn’t work with Preact core because it relies oncreateClass
. There’s hope though: the React team is looking to splitcreateClass
into its own independent library soon 😃@sebinsua eventually, I’ve switched to this solution in my tooltip library: https://github.com/slmgc/react-hint/blob/master/src/index.js#L1
There is a factory which returns a component. The factory doesn’t assume which rendering library is being used and expects a compatible interface implementation of
createElement
andComponent
, e.g.: