Javascript React Reducer未更新状态
我被我的第一个react redux应用程序逼疯了。我的Redux状态从未更新。 我试图在网站上找到每一个解决方案,但他们没有帮助。这里使用了Javascript React Reducer未更新状态,javascript,reactjs,redux,reducers,Javascript,Reactjs,Redux,Reducers,我被我的第一个react redux应用程序逼疯了。我的Redux状态从未更新。 我试图在网站上找到每一个解决方案,但他们没有帮助。这里使用了承诺,但我不是。 Redux开发工具捕获有关登录成功的操作,但是全局状态从未更新,如果可以,请告诉我应该如何调试,非常感谢 First Index.js import ... from ... const compostEnhancer = window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ || compose;
承诺
,但我不是。
Redux开发工具捕获有关登录成功的操作
,但是全局状态从未更新,如果可以,请告诉我应该如何调试,非常感谢
First Index.js
import ... from ...
const compostEnhancer = window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ || compose;
const rootReducer = combineReducers({
auth: authReducer,
})
const store = createStore(rootReducer, compostEnhancer(
applyMiddleware(thunk)
));
const app = (<BrowserRouter><App /></BrowserRouter>)
ReactDOM.render(<Provider store={store}>{app}</Provider>, document.getElementById('root'));
registerServiceWorker();
authAction.js:
import * as actionTypes from './actionTypes';
import axios from 'axios';
export const loginStart= () =>{
return {
type: actionTypes.LOGIN_START
};
}
export const loginSuccess = (token,userId) =>{
return {
type: actionTypes.LOGIN_SUCCESS,
userId: userId,
idtoken: token,
}
}
export const loginFail = (error) =>{
return {
type:actionTypes.LOGIN_FAIL,
error:error
}
}
export const auth = (email,password,isSignup ) =>{
return dispatch =>{
dispatch(loginStart());
const authData = {
email: email,
password: password,
returnSecureToken:true
}
let url = 'https://www.googleapis.com/identitytoolkit/v3/relyingparty/signupNewUser?key=...'
if(!isSignup ){
url = 'https://www.googleapis.com/identitytoolkit/v3/relyingparty/verifyPassword?key=...'
}
axios.post(url, authData)
.then( response =>{
console.log(response);
dispatch(loginSuccess(response.data.idToken, response.data.localId))
})
.catch(err=>{
console.log(err);
dispatch(loginFail(err));
})
}
}
Login.js(组件):
导入。。。从…起
类登录扩展组件{
建造师(道具){
超级(道具);
此.state={
电邮:“,
密码:“”,
isSignup:错,
}
this.handleChange=this.handleChange.bind(this);
this.handleSubmit=this.handleSubmit.bind(this);
}
handleSubmit=e=>{
e、 预防默认值();
//这将触发行动
this.props.onAuth(this.state.email、this.state.password、this.state.isSignup);
console.log(this.props.token)//在这里我可以得到token
}
handleChange=(e)=>{
this.setState({[e.target.name]:e.target.value});
}
render(){
形成=
登录
提交
癌变
如果(本道具装载){
表格=
}
让errorMessage=null;
if(this.props.error){
错误信息=(
{this.props.error.message}
)
}
让token=null;
if(此.props.token){
token=this.props.token.toString()
}
返回(
{errorMessage}
{form}
)
}
}
常量mapStateToProps=状态=>{
返回{
加载:state.auth.loading,
错误:state.auth.error,
令牌:state.auth.idToken,
}
}
const mapDispatchToProps=调度=>{
返回{
onAuth:(email,password,isSignup)=>dispatch(actions.auth(email,password,isSignup)),
}
}
导出默认连接(mapStateToProps、mapDispatchToProps)(登录);
此外,该问题还导致我的微调器
和showErrorMessage
不工作 我想这是个打字错误
而不是
switch (action.types)
试试这个
switch (action.type)
在reducer中,我们从操作返回一个对象,在参数action
上,我想这是一个输入错误
而不是
switch (action.types)
试试这个
switch (action.type)
在reducer中,我们得到一个从操作返回的对象,在参数
action
上,您能发布您的错误的最小复制吗?@AnilRedshift我没有任何关于此问题的错误显示。我可以从服务器获取用户令牌。我试图将令牌存储到reducer,但没有任何更改。您能发布您错误的最小复制版本吗?@AnilRedshift我没有任何关于此问题的错误显示。我可以从服务器获取用户令牌。我试图将令牌存储到reducer,但没有任何更改。