What is the current behavior?
So I'm not sure if this is a bug or intended behavior but here is the scenario. I have encapsulated all of our frontend data-layer code and utilities into a separate package that I can then add as a dependency to our various frontend projects (mobile, web, backend, other dashboards, etc).
Problem: when I use If I move the custom hook (and it's What is expected behavior? VersionsuseSelector() from within the dependency package (used in a custom hook) it doesn't seem to receive the redux store I passed into the <Provider /> from the main application. I end up with the error "Invariant Violation: could not find react-redux context value; please ensure the component is wrapped in a \useSelector() call) to the main application then everything works as intended. I've tried to make sure that the versions of react and react-redux match both in the main application and dependency package but to no avail.
I would _like_ to be able to encapsulate the useSelector() hook into packages and share them.// package.json
"react": "16.9.0",
"react-native": "0.61.5",
"react-redux": "^7.1.3",
"redux": "^4.0.5",
$ npm ls react react-redux redux
@mycompany/[email protected] /Users/aforty/Code/mycompany-mobile
βββ¬ @mycompany/[email protected]
β βββ [email protected] deduped
β βββ [email protected] deduped
β βββ [email protected] deduped
βββ [email protected]
βββ [email protected]
βββ [email protected]
Yep, that dependency tree shows the problem. Any time there's multiple copies of React or React-Redux in your tree, things are almost definitely going to break. Your own package should likely declare them as peerDependencies (and probably devDependencies), not dependencies. It's the app project's responsibility to make sure they exist at runtime.
Oh yeah I didnβt think of that. Thanks for the tip!
I've noticed this bug can also appear, not because of dups, but because of es vs lib entry point
In my React Native bundle I can notice in the source code there are twice the lib under different paths:
βnode_modules/react-redux/lib/....β
βnode_modules/react-redux/es/....β
I'm not yet sure why and how this happens, if you guys have any idea of a root cause, I might hack something in the node_modules to solve it in my end (like patching the package.json to not mention the "module" entry for now)
We ship the compiled code in both CommonJS and ESM module formats, because both are useful in different build setups.
Whatever bundler you're using _should_ only be using one of those two folders.
mmh so I'm using React Native default bundler. (react-native start)
Nothing about the build output format has changed in quite a while, so I don't have any answers on why you might be experiencing issues.
Ok thanks. If I don't foresee any solution, I'll try to make a minimal reproductible example and send an issue to appropriate place (either react-native or metro)
Ok i've spot the problem, we had wrong import that was doing import connect from "react-redux/es/connect/connect" due to automatic import IDE.. we will make sure no devs use magic importer, this is too error prone.
if it helps anyone passing by.
thanks
Yep, I've seen a couple other people mention that same thing happening.
I've found solution. Add alias into webpack config for 'react-redux'
'react-redux': require.resolve('react-redux'),
@Firanolfind exactly where in the webpack file should i add 'react-redux': require.resolve('react-redux')?
@brayanL you can do it here:
...
resolve: {
...
alias: {
'react-redux': require.resolve('react-redux'),
},
},
I'm also having this error, is there any updates about this issue?
Per the comments I made earlier, there are no "updates". This is not a bug in React-Redux - it's always an issue with your build environment.
Same problem here.
import { configure } from "enzyme";
import Adapter from "enzyme-adapter-react-16";
configure({ adapter: new Adapter() });
import configureStore from "redux-mock-store";
const mockStore = configureStore();
let store = mockStore(initialState);
import CarPhoto from "../CarPhoto";
import React from "react";
import { shallow } from "enzyme";
it("renders without crashing", () => {
shallow(
<Provider store={store}>
<CarPhoto />
</Provider>
);
});
```
console.error node_modules/jest-environment-jsdom/node_modules/jsdom/lib/jsdom/virtual-console.js:29
Error: Uncaught [Error: could not find react-redux context value; please ensure the component is wrapped in a <Provider>]
simple CRA app, `"react-scripts": "3.4.1",`
component:
```js
import React, { useEffect, useCallback } from "react";
import { useDispatch, useSelector } from "react-redux";
import CarPhoto from "./CarPhoto";
import { getCar } from "./carActions";
import { selectPhotos } from "./carSelectors";
import Container from "@material-ui/core/Container";
import { Helmet } from "react-helmet";
import Typography from "@material-ui/core/Typography";
import Grid from "@material-ui/core/Grid";
import { makeStyles } from "@material-ui/core/styles";
const CarPhotos = (props) => {
const useStyles = makeStyles((theme) => ({
container: {
paddingTop: theme.spacing(2),
paddingBottom: theme.spacing(2),
},
}));
const classes = useStyles();
const dispatch = useDispatch();
const photos = useSelector((state) => selectPhotos(state));
const fetchCar = useCallback(() => {
return dispatch(getCar());
}, [dispatch]);
useEffect(() => {
fetchCar();
}, [fetchCar]);
if (photos && photos.length) {
return (
<Container maxWidth="lg" className={classes.container}>
<Helmet>
<meta charSet="utf-8" />
<title>Photos</title>
</Helmet>
<Grid container spacing={2}>
<Grid item>
<img src="logo.png" />
</Grid>
<Grid item display="flex">
<Typography variant="h2" component="h2">
Coding Challenge
</Typography>
</Grid>
</Grid>
<Grid container spacing={2} justify="center">
{/* using the array index as key as it will probaly wont change and we're not doing table sorting or filtering */}
{photos.map((photo, index) => (
<Grid key={index} item xs={12} sm={6} md={3}>
<CarPhoto photo={photo} withReadMoreButton={true} />
</Grid>
))}
</Grid>
</Container>
);
} else {
return null;
}
};
export default CarPhotos;
Hi how should i get rid of this ?
βΆ npm ls react react-redux redux
[email protected] /Users/luca/Code/Code_Taff/ts
βββ¬ @types/[email protected]
β βββ [email protected]
βββ¬ [email protected]
β βββ¬ [email protected]
β β βββ [email protected] deduped
β βββ [email protected] deduped
βββ [email protected]
βββ [email protected]
Coz i think this lead me to have a lot of bulls**t in my app and is for the work not for a side project
Get rid of... what, exactly?
This appears to be an NPM usage question, and isn't something we can support here. Please ask that in a relevant venue.
Most helpful comment
I've found solution. Add alias into webpack config for 'react-redux'
'react-redux': require.resolve('react-redux'),