2

I know that are a similar question here (React Native - Global Event Listener), but my App.js is not a component itself, instead is where I initialize the Navigation (react-native-navigation) with a component and redux.

My question is: How I could call redux actions from Bluetooth listener if I'm not inside any component yet?

const store = configureStore();

Navigation.registerComponent("printerapp.ExampleScreen", () => ExampleScreen);

 Navigation.registerComponent(
   "printerapp.SelectPrinterScreen",
   () => props => (
    <Provider store={store}>
       <SelectPrinterScreen {...props} />
     </Provider>
   ),
   () => SelectPrinterScreen
 );

 Navigation.registerComponent(
   "printerapp.SharePlaceScreen",
   () => props => (
     <Provider store={store}>
       <SharePlaceScreen {...props} />
     </Provider>
   ),
   () => SharePlaceScreen
 );

 // ... other component registrations.      

 BluetoothSerial.on("bluetoothDisabled", () => {
   alert("Bluetooth Disabled");
   // HERE WANT TO DISPATCH REDUX ACTION
   // BUT I DONT KNOW HOW MAKE THAT IF THAT IS NOT A COMPONENT
 });

 // Start app
 export default () => {
        Navigation.setRoot({
           root: {
             stack: {
                 children: [
                 {
                     component: {
                     name: "printerapp.SelectPrinterScreen"
                     }
                 }
                 ],
                 options: {
                     topBar: {
                         title: {
                             text: "Bluetooth printer"
                         }
                     }
                 }
             }
           } 
         });
}
sevenlops
  • 456
  • 1
  • 4
  • 18
  • You can wrap Start app section in a class and define global listeners in the constructor – Pritish Vaidya Feb 12 '19 at 13:52
  • 1
    import `store` and do `store.dispatch(YourAction)` – Ziyo Feb 12 '19 at 14:10
  • @Ziyo you saved my day. I learned how to call actions and get store only from a Component connected with Redux, but no outside. One more thing: Could I remove and set back this global listeners somehow? I mean, inside Redux store mehod, for example, I want to remove a global listener and then, inside a Component method, set back. Thank you man! – sevenlops Feb 12 '19 at 18:49
  • You can do anything. Redux is not magic, it is a huge javascript object. – Ziyo Feb 13 '19 at 14:22
  • @Ziyo I know but how? How I cant remove and set up this global listener method? – sevenlops Feb 13 '19 at 15:37
  • Sorry, I am having a hard time understanding what you trying to do. – Ziyo Feb 13 '19 at 15:48

0 Answers0