score:89
for reusable components:
- put a
react
dependency in bothpeerdependencies
anddevdependencies
. - never put a
react
dependency independencies
.
peerdependencies
specifies which version(s) of react your reusable component supports/requires. when using npm 2 this also adds react to the list of modules to be installed, but this is no longer the case with npm 3.
devdependencies
ensures react will be installed when you run npm install
while developing your component, or when running tests on travis or similar.
putting react
in dependencies
will cause multiple versions of react to be installed if somebody uses your component but has a different version of react in their own package.json
- having multiple versions of react not only bloats the build, but also causes errors when different versions try to interact.
score:-3
you can have react
in either peerdependencies
or in dependencies
. the difference being that with a peerdependencies
, react
is only installed once for the package using your package. if you put it in dependencies
, react
will be installed twice, one time for the package using your package, and once time for your package.
react itself seems to favor peerdependencies
for some reason. you obviously don't want two separate versions of react
in your javascript bundle (which happens by default if you use dependencies
), but that is easy to fix with npm dedupe
.
so there's no correct way to do it, both peerdependencies
and dependencies
work. using dependencies
is more in line with the node/npm way, but using peerdependencies
is friendlier to users of your package that doesn't know about npm dedupe
and why it's needed.
score:9
the selected answer is definitely the prescribed approach here however i've started favoring the use of inversion of control as opposed to relying on npm peer dependencies for my libraries dependencies and its served me well.
libraries are easier if you build them functional. it seems to be easier to maintain libraries that export a single function which takes in an object with all of their heavy dependencies and export an object containing each of your libraries typical exports.
library 'injected'
lib/index.js
export default ({ react }) => {
const injectedcomponent = props => (
<p style={{color: props.color}}>this component has no react npm dependencies.</p>
)
/** other stuff */
return { injectedcomponent }
}
consuming app
app.js
import react from 'react'
import { render } from 'react-dom'
/** import the default export factory from our library */
import createinjectedcomponent from 'injected'
/** call the factory, passing its dependencies (guaranteed to match what we're bundling) and get back our component */
const { injectedcomponent } = createinjectedcomponent({ react })
render(<injectedcomponent color="blue" />, document.getelementbyid('root'))
if your component only works with a given version of react or some other dependency, you can write some assertions around the version for the react parameter that is passed in. overall, building libraries in this fashion should be less prone to new build issues appearing anytime version of react is published and will more importantly ensure that you are not causing your library consumers to bundle multiple versions of react and other heavy libraries. this pattern works well with npm
link (i generally have 16+ libraries running from npm
link simultaneous and experienced issues when i didn't use this pattern).
in your main app i would recommend always splitting out react
, react dom
and any react lib components you use into a vendor bundle (webpack) and mark it as external in your main bundle so that you do not unintentionally bundle two versions.
Source: stackoverflow.com
Related Query
- What is the correct way of adding a dependency to react in your package.json for a react component
- What is the correct type for React Click Event?
- what is the correct type for a React instance of a component in typescript
- What is the correct way to animate/transition between routes in react router
- What is the correct way to change Navbar values in React based on if user is logged in?
- What is the correct way to define a React component's contextTypes in TypeScript?
- What is the correct typescript type for react children?
- What is the correct pattern for utility function when using React hooks?
- What is the best way for connecting Django models choice fields with React js select options
- What is the correct way to type a React HOC?
- What's the correct way to do inheritance in TypeScript for React components?
- What is the correct type to use for this React form field hook?
- What is the best way to include css files for Prime React components when using Parceljs?
- What is the best way to create component library for both React and Preact?
- What is the proper way to fetch JSON in React with Hooks?
- What is the correct syntax for adding the xframe header module to cra rewired?
- What is the React way of adding active class and removing others on hover in li elements
- React - What is the correct way to make an API call with user input?
- What is the correct way to initiate a side-effect on page load when using React Strict Mode?
- What is the correct way to React Hook Fetch Api and store to Redux and retrieve it with Selector immediately?
- What is the correct way to include React in both an application and a private library? (React Invalid Hook Call warning from duplicate React)
- What is the correct way to update a counter in React
- What is the correct way to pass an argument to parent component in React from onClick event handler
- What is the best way to create a Loading Animation for React JS Ajax calls?
- What is the correct way to use react hook useState update function?
- What is the correct way to set state in React
- What is the correct way of updating state for which the value needed to be a combination of previous state and event
- What is the correct way to add to an array with splice in react
- What is the correct way to setup an independent Route that is not a part of App.js in React Router Dom?
- What is the correct way to type a React component class that has no props?
More Query from same tag
- How do I get the full Quill Api?
- Change Text field of Material UI from black to white
- Cannot display GET method to modify and update data with formData
- React Importing a component folder
- Optimized webpack bundle depending on browser and browser version?
- Go to NavLink when onKeyDown function is run
- Error in React/Redux - Expected an assignment or function call
- React Duplicate components updating wrong state: hooks
- How to iterate over components inside components in React
- Pushing react app to Heroku
- Generic props : why are these types incompatible?
- Number of objects vs Payload, while scaling a modern Javascript project which is more important?
- How are properties defined on React elements using JSX 'collected' into a single object?
- React Material UI Drawer — Sliding Transition not working
- Socket IO with React and Express. ReactJS Error: Could not proxy request /socket.io/?EIO=4..... from localhost:3000 to http://127.0.0.1:4000
- How to NOT resolve paths with Webpack?
- How to retrieve the parameter after a hash (#) with React Router and useParams hook?
- React - read dates from excel file
- Material UI useMediaQuery causes glitching/jumping
- I am trying to set data in state everytime on first index for react in array of object
- How to use Redux devtools with Nextjs?
- How to pass a unique id as a prop to another component
- How to integrate React-app-rewired with Customize-CRA
- Conditional lazy loading import timeago languages in React.js
- React Admin dataProvider not sending id during create
- Konva continue to track mouse outside stage
- filter remove an element from an array in react
- Is it possible to pass the Parent instance to a Child compontent into props attributes?
- Changing HTML tag after two index while Mapping an Array
- Generate two or more items returned by function map in Javascript