Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Reactjs 在react中使用withRouter从webservice获得响应后,我想重定向到特定页面_Reactjs_Typescript_React Redux - Fatal编程技术网

Reactjs 在react中使用withRouter从webservice获得响应后,我想重定向到特定页面

Reactjs 在react中使用withRouter从webservice获得响应后,我想重定向到特定页面,reactjs,typescript,react-redux,Reactjs,Typescript,React Redux,我需要帮助理解在我使用react中的axios和使用typescript的redux从web服务获得响应后如何重定向到页面。 在我的例子中,即使在生成响应之前,页面也会直接重定向到新页面。 下面是同样的代码片段 ##Component.jsx## import*as React from'React'; 从“../../../Constants”导入{Constants}; 从“react router dom”导入{withRouter}; 导入“../styles/LoginPage.cs

我需要帮助理解在我使用react中的axios和使用typescript的redux从web服务获得响应后如何重定向到页面。 在我的例子中,即使在生成响应之前,页面也会直接重定向到新页面。 下面是同样的代码片段

##Component.jsx##

import*as React from'React';
从“../../../Constants”导入{Constants};
从“react router dom”导入{withRouter};
导入“../styles/LoginPage.css”;
从“../../../store”导入{store};
从“/../actions/LoginAction”导入addSessionID;
const logo=require('../../../assets/images/logo.png');
const footer=require('../../../assets/images/veras logo.png');
类登录扩展了React.Component{
公共引用:{用户名:HTMLInputElement,
密码:HTMLInputElement};
构造器(道具:任何){
超级(道具);
this.onSubmit=this.onSubmit.bind(this);
}
onSubmit(事件:任意):无效{
event.preventDefault();
常量头:任意={
“授权”:“基本”+window.btoa(this.refs.username.value+):“+this.refs.password.value),
“内容类型”:“应用程序/json”
};
常量URL=
Constants.VERAS_API.SESSION.BASE_URL+
Constants.VERAS_API.SESSION.LOGIN+
Constants.VERAS_API.SESSION.APPLICATION_ID_字符串;
dispatch(addSessionID(URL,headers));
//this.props.history.push('/Dashboard');
}
render(){
const username='username';
const password='password';
返回(
{Constants.LANGUAGES.EN_US.LOGIN.LABELS.LOGIN_FORM}
  • {Constants.LANGUAGES.EN_US.LOGIN.LABELS.USERNAME_FIELD}
  • {Constants.LANGUAGES.EN_US.LOGIN.LABELS.PASSWORD_FIELD}
{Constants.LANGUAGES.EN_US.LOGIN.LABELS.forget_PASSWORD_LINK} {Constants.LANGUAGES.EN_US.LOGIN.LABELS.LOGIN_BUTTON} ); } }
使用路由器导出默认值(登录)您可以检测道具中的更改,然后可以重定向到组件中的页面,如

componentWillReceiveProps(newProps){
    if(newProps.login_status){ // Redirect if login status become true
       this.props.history.push('/Dashboard')
    }
}
或者您可以签入
render
方法,如

render() {
    if (this.props.login_status) { // Redirect if login status true
         this.props.history.push('/Dashboard');
    }
    return <LoginComponent /> // Component name changed for understanding
}
render(){
if(this.props.login_status){//如果登录状态为true,则重定向
this.props.history.push('/Dashboard');
}
return//Component name为便于理解而更改
}

检查此答案谢谢,我还有一个疑问。在我的reducer中设置的初始状态为“session_id”为“”,当我单击login按钮时,它会给我存储在redux中的session_id,即为空,因为我没有收到web服务的任何响应。如果我再次单击该按钮,它会给我上一个会话id,并存储来自web服务的新会话id。那么我该如何解决这个问题呢?@Advait-您仍然获得了上一个会话id而不是当前会话id?你从商店里得到了什么?抱歉,如果我理解错误。我正在将日志粘贴到评论中:-内部调度登录。tsx:38===={session_id:,登录状态:false,错误:}LoginReducer.tsx:11 Reducer bee03a9822824f8eb12a4773e287e4bb LoginReducer.tsx:15{session id:“bee03a9822824f8eb12a4773e287e4bb”,登录状态:true,错误:}登录。tsx:38==={session id:“bee03a9822824f8eb12a4773e287e4bb”,登录状态:true,错误:}登录还原程序。tsx:11还原程序f0496644d3344d3fbfd8e73854de91bc登录还原程序。tsx:15{会话id:“f0496644d3344d3fbfd8e73854de91bc”,登录状态:true,错误:}我也不能使用“调度(推送('/Dashboard'))“。它不是重定向,而是给我dispatch@Advait-在减速机中包装初始状态,如
返回{…状态,会话id:action.payload,登录状态:true,错误:'}
,然后尝试并
调度(推送(“/Dashboard”)
仅在使用
react router redux
时有效。因此,请尝试上面提供的其他选项。