How to write to Redux store outside of a React component?

Mr. Robot

I have a React Native app where I am using HeadlessJS to call a handler on receipt of a Firebase Cloud Messaging notification.

Inside my handler which is a function, not a React component, I am accessing the Redux store using the following method:

import store from '../redux/store';

const backgroundNotificationHandler = async message => {

  const state = store.getState();
...

My question is, how can I update the store in a a way that isn't a 'hack'?

Currently I have the following line in the function:

state.user.wokenFromBackgroundListener = true;

Surprisingly is works, but this is without dispatching an action or using a reducer.

Is there a better way to do it?

I can't dispatch an action because the function is not a component and it is not a component because it can't be - it requires a function as in the docs.

My code in index.js is:

AppRegistry.registerComponent(appName, () => App);
firebase.messaging().setBackgroundMessageHandler(backgroundNotificationHandler);
Max

Dispatching from component props is just an added functionality provided by react-redux. You can still use plain redux api, in this case you can dispatch to store using store.dispatch

store.dispatch({ type: 'SOME_ACTION', payload: [1,2,3] })

However I'm not sure if you should be doing that, I haven't used HeadlessJS myself but I would first check and make sure that these task handlers are actually being run in the same context your app is running (e.g. confirm that they share store instance with your app, and NOT create a separate store just because you import store in file with the handler)

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

React Native setState in component updating redux store

分類Dev

How to access props outside of a class component React

分類Dev

When to add something to the redux store versus the component specific store when using React Redux

分類Dev

How to test a component with a nested container with React and Redux?

分類Dev

How to connect the Redux's store and actions to React's components

分類Dev

How to make a React component setState() based on a Redux state change?

分類Dev

Component not updating when redux store modified

分類Dev

Connecting Redux Store to Component to populate map with markers

分類Dev

writing function outside of a class in react component

分類Dev

Detect click outside React component using hooks

分類Dev

How to write an ES6 class React Component that extends a functional component?

分類Dev

React Redux store state change is not working

分類Dev

Unable to display data in component with react and redux

分類Dev

React component not updating on redux state update

分類Dev

React component does not update with the change in redux state

分類Dev

unable to receive the state in react component from redux

分類Dev

Calling React component method of redux-form

分類Dev

React + Redux PUT api request to edit data in my redux store

分類Dev

How to handle dialog state outside of dialog component?

分類Dev

React: Possibility to store redux store and states into local file?

分類Dev

useEffect in React not update component after update store

分類Dev

How to manage custom aspects of a React component (Redux, CSS) whilst distributing via NPM

分類Dev

Redux-observable: how a component can subscribe to react when action is completed?

分類Dev

react component connected, redux state changes... but no update to component?

分類Dev

Update component state from outside React (on server response)

分類Dev

React set Hook from function call outside Component

分類Dev

Where to declare auxiliar functions for a React functional component. Inside or outside?

分類Dev

Performance difference from putting function outside of React component?

分類Dev

How to extend a React component?

Related 関連記事

  1. 1

    React Native setState in component updating redux store

  2. 2

    How to access props outside of a class component React

  3. 3

    When to add something to the redux store versus the component specific store when using React Redux

  4. 4

    How to test a component with a nested container with React and Redux?

  5. 5

    How to connect the Redux's store and actions to React's components

  6. 6

    How to make a React component setState() based on a Redux state change?

  7. 7

    Component not updating when redux store modified

  8. 8

    Connecting Redux Store to Component to populate map with markers

  9. 9

    writing function outside of a class in react component

  10. 10

    Detect click outside React component using hooks

  11. 11

    How to write an ES6 class React Component that extends a functional component?

  12. 12

    React Redux store state change is not working

  13. 13

    Unable to display data in component with react and redux

  14. 14

    React component not updating on redux state update

  15. 15

    React component does not update with the change in redux state

  16. 16

    unable to receive the state in react component from redux

  17. 17

    Calling React component method of redux-form

  18. 18

    React + Redux PUT api request to edit data in my redux store

  19. 19

    How to handle dialog state outside of dialog component?

  20. 20

    React: Possibility to store redux store and states into local file?

  21. 21

    useEffect in React not update component after update store

  22. 22

    How to manage custom aspects of a React component (Redux, CSS) whilst distributing via NPM

  23. 23

    Redux-observable: how a component can subscribe to react when action is completed?

  24. 24

    react component connected, redux state changes... but no update to component?

  25. 25

    Update component state from outside React (on server response)

  26. 26

    React set Hook from function call outside Component

  27. 27

    Where to declare auxiliar functions for a React functional component. Inside or outside?

  28. 28

    Performance difference from putting function outside of React component?

  29. 29

    How to extend a React component?

ホットタグ

アーカイブ