D
D
Denis Sokolov2020-12-16 00:39:17
React
Denis Sokolov, 2020-12-16 00:39:17

Why does this work and this does not React?

// Это сохраняет и работает
state = {
        user: false
    }

    componentDidMount() {
        fetch('http://localhost:3000/auth/login')
            .then(res => res.json())
            .then(res => this.setState({user: res}))
            .then(() => console.log(this.state.user))
            .catch(e => console.log(e))
    }


// Это не сохраняет в user и не работает
const [user, setUser] = useState({})

useEffect(() => {
 fetch('http://localhost:3000/auth/login')
            .then(res => res.json())
            .then(res => setUser(res))
            .catch(e => console.log(e))
}, [])

Answer the question

In order to leave comments, you need to log in

1 answer(s)
L
Leonid Yakovlev, 2020-12-16
@deleo547

Why should this work if you don't use the user variable in the hook example?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question