Answer the question
In order to leave comments, you need to log in
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
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 questionAsk a Question
731 491 924 answers to any question