gpt4 book ai didi

javascript - 使用react和redux登录api请求

转载 作者:行者123 更新时间:2023-11-28 18:28:39 28 4
gpt4 key购买 nike

我正在尝试使用 react 和 redux 制作一个小项目。该项目的Main页面是login。我已经有一个用于登录的工作 api。以下是我的项目中的代码片段:

login.js

onLoginFormSubmit(event) {
event.preventDefault();
this.props.actions.login(this.state.username, this.state.password);
}

render() {
return (
...Login Form ...
);
}
}

Login.propTypes = {
actions: PropTypes.object.isRequired,

};

function mapStateToProps(state, ownProps) {
return {
loginResponse: state.loginResponse
};
}

function mapDispatchToProps(dispatch) {
return {
actions: bindActionCreators(loginAction, dispatch)
}
}

export default connect(mapStateToProps, mapDispatchToProps)(Login);

loginReducer.js

import {
LOGIN_SUCCESS,
LOGIN_FAILED,
LOGIN_ATTEMPT
} from '../actions/type';
import Immutable from 'immutable';

const initialState = new Immutable.Map({
username: '',
password: '',
isLoggingIn: false,
isLoggedIn: false,
error: null
});

export default function user(state = initialState, action){
switch (action.type){
case LOGIN_ATTEMPT:
console.log("LOGIN_ATTEMPT: ",action.user);
return state.merge({
isLoggingIn: true,
isLoggedIn: false,
username: action.user.username,
password: action.user.password
});

case LOGIN_FAILED:
console.log("LOGIN_FAILED: ");
return state.merge({
error: action.error,
isLoggingIn: false,
isLoggedIn: false
});

case LOGIN_SUCCESS:
console.log("LOGIN_SUCCESS: ",action);
return state.merge({
error: null,
isLoggingIn: false,
isLoggedIn: true
})
break;

default:
return state;

}
}

loginAction.js

export function loginError(error){
return { error, type: LOGIN_FAILED };
}

export function loginSuccess(response){
return dispatch => {
dispatch({ response, type: LOGIN_SUCCESS});
};
}

export function loginRequest(username, password){
const user = {username: username, password: password};
return { user, type: LOGIN_ATTEMPT };
}


export function login(username, password) {
console.log("User Data: ", username, password);
return dispatch =>
fetch('http://192.168.1.100:9090/login', {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
},
body: JSON.stringify({
username: username,
password: password
}),
})
.then(response => {
console.log("I'm here");
if(response.status >= 200 && response.status < 300){
console.log("Response; ", response);
dispatch(loginSuccess(response));
} else {
const error = new Error(response.statusText);
error.response = response;
dispatch(loginError());
throw error;
}
})
.catch(error => { console.log('Request Failed: ', error);});
}

问题是,login api 没有被调用。我在控制台中遇到的唯一错误是未捕获错误:操作必须是普通对象。使用自定义中间件进行异步操作。

我知道有用于异步调用的redux-thunk,但不知道如何让它工作。我还在学习。因此,任何帮助将不胜感激。

谢谢。

最佳答案

<强> DOCS:

npm install --save redux-thunk

然后,要启用 Redux Thunk,请使用 applyMiddleware():

import { createStore, applyMiddleware } from 'redux';
import thunk from 'redux-thunk';
import rootReducer from './reducers';

// Note: this API requires redux@>=3.1.0
const store = createStore(
rootReducer,
applyMiddleware(thunk)
);

关于javascript - 使用react和redux登录api请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38582213/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com