I am trying to use redux in my project and I am have a problem with call a function that using dispatch

OsamaTab

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 .........................................................................................................

Drew Reese

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.

edited at
0

Comments

0 comments
Login to comment

Related

I am trying to use back tick to call out a function that contains my id and key. But I am getting an error as Unexpected token

I am trying to use a function in my project. But the console shows error

I am trying to use a slider component in my angular project i have ran the ng add @angular/material still the following error

Why is my function receiving an error that it is not defined in this scope? I am simply trying to call the function and have it run as of right now

Do I need to use redux-persist if I am trying to use firebase with my react native application?

I am trying to scrape a website for my project but I am stuck with an error. Take a look and let me know if anyone have suggestion for this

i am importing a async function and trying to use it

I am trying to use the list I made in my getData function in my normalise function

Is there a problem with my code or the API I am using?

My Grand Central Dispatch usage: Am I using it correctly?

I am trying to display the index value of strings which I have obtained using the input function

I am trying to do a project on GTK+ using Glade but when I am compiling my code and running the executable file I am facing some errors

I am doing a training project and have encountered a problem

i am trying to install mongoose to my nodejs project but fail

I am getting error while trying to run my flutter project

I am trying to make and ajax call to my django backend

I am trying to have the color of my menu button that was clicked change after the clicked page is loaded, using jquery

when i am trying to call a servlet page from my jsp page using anchor tag it showing an error

I am trying to call a function within itself, but it does not work for me

I am trying to call an ajax method but this calls error function

here i am getting await can only use inside async function error but i am using async in my function

I am trying to better understand recursion by using the FizzBuzz problem in javascript

I am not able to use mapDispatchToProps in my React + Redux application

I am trying to fit an exponential function to my graph in R

I am trying to toggle my Boolean by IsFormDisplayed function

I am trying to build an image viewer using Tkinter but I am stuck at this weird problem

I have a problem with two .js files in a react app! I am using the react-router-dom. My problem is with the Link tag

When using function calls to template functions, I am faced with an unexpected matching template function for my call

When i am trying to call the api using thunk , i am getting undefined