app.js
import {fetchUser} from '../redux/action/index'
import { useDispatch,useSelector } from 'react-redux'
import { bindActionCreators } from 'redux'
const counter = useSelector(store => store.userState.currentUser)
useLayoutEffect(() => {
fetchData();
fetchUser();
console.log(counter)
}, []);
calling for fetchUser.js
import firebase from "firebase";
import {USER_STATE_CHANGE} from '../constants/index'
export function fetchUser() {
return( async(dispatch)=>{
console.log('dssd')
firebase.firestore()
.collection("users")
.doc(firebase.auth().currentUser.uid)
.get()
.then(
(snapshot)=>{
if(snapshot.exists){
dispatch({type: USER_STATE_CHANGE , currentUser : snapshot.data()})
}
else{
console.log('error_fitchUser')
}});
})
}
user.js
import {USER_STATE_CHANGE} from '../constants/index'
const initialState={
currentUse:null,
}
export const user=(state=initialState,action)=>{
switch (action.type) {
case USER_STATE_CHANGE:
return{
...state,
currentUser:action.currentUser,
}
default:
return({})
}
}
the problem is that I don't Know how to send dispatch when calling for fetchUser .........................................................................................................
Presumably you've already added your asynchronous action middleware to your store. You can use the useDispatch
react-redux hook and wrap your calls to your fetchData
and fetchUser
action creators with a call to dispatch
. The middleware will handle passing dispatch
to the asynchronous action.
const dispatch = useDispatch();
useLayoutEffect(() => {
dispatch(fetchData());
dispatch(fetchUser());
}, []);
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments