My organization created a big RN app. Now they planned to create multiple other products. some products have similar kinds of functionality components. But redux has been used widely for calling API, maintaining state, and updating UI.
I have created my own npm package in which I placed common components so that other applications can use them. I don’t want redux dependencies should be there in common components. I want to access the redux store/provider from the npm package components. Is it possible?
Package.json file in dependencies,
"dependencies": {
"react-native-ui": "git+https://github.myorg.com/myOrg/orgUI.git#feature/UIComponents",
In app.tsx,
import { MyCommonComponent } from 'react-native-common-ui';
return (
<Provider store={store}>
<MyCommonComponent />
</Provider>
);
};
Is it possible store is accessible in my MyCommonComponent
?
2
Answers
better copy npm package as your library, you can access redux easy
you can pass the Redux store as a prop to the components in your npm package, making them independent of Redux while still being able to interact with the store when used in an application.
Here’s how you can modify your npm package components to accept the Redux store as a prop:
In your npm package, modify the component to accept a store prop:
In the application where you are using the npm package components, pass the Redux store as a prop to the component: