Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

182
Views
Don't know how to properly access fetching result in redux state with hooks

I'm trying to fetch data from my backend to my redux store to than access it in my functional React Component. (Using hooks instead of classes)

When I'm trying to fetch the data in the component itself like this:

useEffect(() => {
  const fetchCategories = async () => {
    const res = await trees.get('/categories')
    setCategories(res.data)
  }

  fetchCategories()
}, [])

it works and i have access to the data, but it's obviously not in the redux store, just in my components state. (I have my axios.create in a seperate file, so I can just fetch with tree.get)

And fetching in my action creator works aswell, I can successfully dispatch the responses data into my redux store like this:

export const fetchCategories = async () => {
  const res = await trees.get('/categories')
  dispatch({ type: FETCH_CATEGORIES, payload: res.data })
}

state in my redux store

But I don't know how to properly run my action creator to than access the data in the redux store from my React Component. I tried several things like:

const [categories, setCategories] = useState([{
    "value": "loading...",
    "icon": "music"
  }])

  useEffect(() => {
    const plsWork = async () => {
      const res = await fetchCategories()
      console.log(res)
    }

    plsWork()
  }, [])

or the useSelector Hook from redux, but I couldn't figure out a way, that's working. :(

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

You used useDispach hook to update the redux store data and you should use useSelector hook to get the state data you want.

const categories = useSelector((state) => state.categories)
over 4 years ago · Santiago Trujillo Report

0

You have to pass dispatch too as you can use useDispatch hook only in functional component. So for this:

export const fetchCategories = async ()=> (dispatch)=> {
  const res = await trees.get('/categories')
  dispatch({ type: FETCH_CATEGORIES, payload: res.data })
}

Then in component where you want to access the state you should use useSelector hook

const dispatch=useDispatch(); 

const categories=useSelector((state)=>state.reducerName.categories); // if you have more than one reducer write here reducername which you have assigned it in the combineReducer or if you are using only single reducer then you can also access it simply by state.categories

  useEffect(() => {
    
       dispatch(fetchCategories());
 

  
  }, [])
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!