I'm working on a login function in e-commerce React app using Context API. So when user types in his e-mail and password that calls an action in reducer to check that data and set user data as state in Context Provider.
Here's the action (I'm ommitting other cases that work):
const reducer = (state, action) => {
...
switch(action.type) {
case 'USER_LOGIN':
let user = userLogin(state.user,action.payload);
return {
...state,
user: user,
loggedIn: temp.length> 0 ? true: false
};
...
Here's the function userLogin()
which itself works fine and returns a nice array of user data.
var userLogin = (user, payload) => {
const url = "/api/users/login.php";
console.log(payload);
fetch(url,{
method: "POST",
body: JSON.stringify(payload)
})
.then(response => response.json())
.then(
(result) => {
user.id = result.id;
user.name = result.name;
user.email = result.email;
user.phone = result.phone;
user.city = result.city;
user.street = result.street;
user.building = result.building;
user.flat = result.flat;
},
(error) => {
console.log(error);
});
return user;
}
But when in result in my <Provider>
the value of state of user
stays an empty array as initialized.
I think that there's something that has to work with asyncronous type of that fetch()
, but couldn't find any reference about that.
UPD: this is my <Login>
component
import React, { Component } from 'react'
import {Consumer} from '../../Context'
export default class Login extends Component {
constructor(props) {
super(props);
this.state = {
email: "",
password: "",
};
}
userLogin = (dispatch, e) => {
e.preventDefault();
dispatch({
type: 'USER_LOGIN',
payload: {email: this.state.email,
password: this.state.password }
});
}
handleChange = (e) => {
this.setState({
[e.target.name] : e.target.value
})
}
render() {
return (
<Consumer>
{
value=> {
const {dispatch} = value;
return (
<div className="checkout__container">
<h5 className="checkout__header">Login</h5>
<form>
<label className="checkout__label">E-mail:</label><br />
<input className="checkout__input" type="email" name="email" onChange={this.handleChange}></input><br /><br />
<label className="checkout__label">Password:</label><br />
<input className="checkout__input" type="password" name="password" onChange={this.handleChange}></input><br /><br />
<button type="button" className="button button-primary" onClick={this.userLogin.bind(this, dispatch)}>Sign In</button>
</form>
</div>
);
}
}
</Consumer>
)
}
}
UPD #2: Here's extract from my Context.js
with Provider
component
import React, { Component } from 'react'
import axios from 'axios'
const Context = React.createContext();
const reducer = (state, action) => {
...
switch(action.type) {
case 'USER_LOGIN':
let user = userLogin(state.user,action.payload);
return {
...state,
user: user
};
};
};
var userLogin = (user, payload) => {
// Do fetch() here and return user array
}
...
class Provider extends Component {
constructor(props) {
super(props);
this.state = {
user: [],
loggedIn: false,
dispatch: action => this.setState( state => reducer(state,action))
};
}
render() {
return (
<Context.Provider value={this.state}>
{this.props.children}
</Context.Provider>
)
}
}
const Consumer = Context.Consumer;
export {Provider, Consumer};