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 })
}
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. :(