D
D
Denis Sokolov2021-01-31 21:08:17
React
Denis Sokolov, 2021-01-31 21:08:17

How to push in state redux?

How to push a custom hook into state redux that checks if the user is authorized or not?

import {useEffect, useState} from 'react'
import {localURL} from "../const";

export const useAuth = () => {
    const [user, setUser] = useState({})

    useEffect(() => {
        fetch(`${localURL}/auth/login`)
            .then((res) => res.json())
            .then((res) => {
                setUser(res.user)
            })
            .then((res) => console.log(user))
            .catch((e) => console.log(e));
    }, [])

    return user
}

const initialState = {
    user: {}
}

export default function rootReducer(state = initialState, action) {

    return state
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
abberati, 2021-01-31
@abberati

No way. Store for data, no need to store functions in it. That's what it says in the doc. Rethink your approach to problem solving.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question