-
Notifications
You must be signed in to change notification settings - Fork 38
Implementation with redux #51
Comments
I'm not sure exactly what they meant, but my guess would be that you initialize your proximity observer in the direct child of your redux My best guessApp.js // ...
import * as RNEP from '@estimote/react-native-proximity'
export default () => {
return (
<Provider store={store}>
<Observer />
</Provider>
)
}
const Observer = () => {
const dispatch = useDispatch()
useEffect(() => {
// set up listener here
const zone = new RNEP.ProximityZone(5, 'kitchen')
zone.onEnterAction = (context) => {
dispatch({ enteredKitchen: true })
}
zone.onExitAction = (context) => {
dispatch({ enteredKitchen: false })
}
}, [dispatch])
return <App />
} What I would doIf you're like me, you like putting this kind of logic into custom react hooks, so that you can easily refactor code. App.js import * as React from 'react'
import { useObserver } from './use-observer.js'
import { Provider } from 'react-redux'
import { store } from '../path/to/your/store'
export default () => {
return (
<Provider store={store}>
<Observer />
</Provider>
)
}
const Observer = () => {
useObserver()
return <App />
} use-observer.js import * as RNEP from '@estimote/react-native-proximity'
import { useDispatch } from 'react-redux'
export const useObserver = () => {
const dispatch = useDispatch()
useEffect(() => {
// set up listener here
const zone = new RNEP.ProximityZone(5, 'kitchen')
zone.onEnterAction = context => {
dispatch({ enteredKitchen: true })
}
zone.onExitAction = context => {
dispatch({ enteredKitchen: false })
}
}, [dispatch])
} |
Thanks for the suggestion @nandorojo. However i am not planning to implement react hooks onto the project. However i've tried before dispatching action to modify states in redux store in background. It seems like i cannot access the state or props (normally we access the states thru mapStateToProps/mapDispatchToProps) to execute the functions that requires redux state. Therefore i might try to put the listener at the bottom level of the stack navigator (HomePage) to experiment first |
You could also access the store object directly, without doing it through props, if you’re using background mode for beacons. |
any documentation as reference? @nandorojo |
https://daveceddia.com/access-redux-store-outside-react/ store.js import { createStore } from 'redux';
import reducer from './reducer';
export const store = createStore(rootReducer) observer.js import { store } from '../path/to/store'
...
zone.onEnterAction = context => {
store.dispatch({ enteredKitchen: true })
} You'll probably want to implement |
Hi, thanks for the reference. But for that case it was a headless task. I will try experiment more on that, thanks |
This might help: rt2zz/redux-persist#1066 It appears that you can dispatch actions, not sure about accessing state. |
Thanks for the suggestion. i manage to access the states and actions when initialising the redux store with the app at App.js.
|
Hi, can you explain futher the statement above? Regarding Proximity Observer can be initialised during redux store creation, does the start() initialises at
store.js
orindex.js
where the redux store is integrated on the app?The text was updated successfully, but these errors were encountered: