我正在使用反应路由器4不对路由器3做出反应,所以我使用react-router-dom。我试图让this.props.history.push工作,但它所做的只是保持视图相同,但URL更改。
例如,我单击按钮,它调用一个只调用this.props.history.push('/ posts')的函数。网址从localhost:3000 / signin更改为localhost:3000 / posts。但我仍然在观察。
如果我点击刷新,它会重定向到帖子页面。我已经看了其他问题,那些人无论如何都无法去那里,即使他们刷新或进入网址并点击进入。
我会发布下面的代码,看看我做错了什么。另外我使用的是反应路由器v4而不是3.我不能使用browserHistory.push和v3使用的其他东西。
代码:
成分:
import React, { Component } from 'react';
import {Field, reduxForm} from 'redux-form';
import {connect} from 'react-redux';
import * as actions from '../../actions';
import {withRouter} from 'react-router';
class Signin extends Component {
constructor(props) {
super(props);
this.onSubmit = this.onSubmit.bind(this);
}
renderField(field) {
const { meta: {touched, error} } = field;
const className = `form-group ${touched && error ? 'has-danger' : ''}`;
return (
<div className={className}>
<label><strong>{field.label}:</strong></label>
<input
className="form-control"
type={field.type}
{...field.input}
/>
<div className="text-help">
{ touched ? error : ''}
</div>
</div>
)
}
onSubmit({email, password}) {
// this.props.signinUser({email, password}, () => {
this.props.history.push('/posts');
// });
}
renderAlert() {
if(this.props.errorMessage) {
return (
<div className="alert alert-danger">
<strong>Oops</strong> {this.props.errorMessage}
</div>
)
}
}
render() {
const { handleSubmit } = this.props;
return (
<form onSubmit={handleSubmit(this.onSubmit)}>
<Field
label="Email"
name="email"
type="email"
component={this.renderField}
/>
<Field
label="Password"
name="password"
type="password"
component={this.renderField}
/>
{this.renderAlert()}
<button type="submit" className="btn btn-success">Sign In</button>
</form>
);
}
}
function validate(values) {
const errors = {};
//Validate the inputs from values
if(!values.email) {
errors.email = "Enter an email!";
}
if(!values.password) {
errors.password = "Enter a password!";
}
//If errors is empty, the form is fine to submit
//If errors has any properties, redux form assumes form is invalid
return errors;
}
function mapStateToProps(state) {
return {
errorMessage: state.auth.error
};
}
export default reduxForm({
validate,
form: 'signin'
})(connect(mapStateToProps, actions)(withRouter(Signin)));
行动:
export function signinUser({email, password}, cb) {
return function(dispatch) {
axios.post(`${ROOT_URL}/signin`, {email, password})
.then((response) => {
dispatch({type: AUTH_USER});
localStorage.setItem('token', response.data.token);
})
.then(() => cb())
.catch((error) => {
dispatch(authError(error.response.data.error));
})
}
}
答案 0 :(得分:2)
我发现问题不在我发布的代码中,而是我在做我的路线。确保在路由时,您没有将路由嵌套为路径的组件。例如,我有:
const Routes = (
<Provider store={store}>
<BrowserRouter>
<App />
</BrowserRouter>
</Provider>
)
这里我将App作为一个组件导入(或者它作为组件导出),我将它从组件更改为普通函数。为了更清楚,我将在下面放置旧代码和新代码:
旧:
class App extends React.Component {
render() {
return (
<div>
<Switch>
<Route exact path='/' component={Home} />
<Route path="/signin" component={RequireUnAuth(Signin)} />
<Route path="/signout" component={Signout} />
<Route path="/signup" component={Signup} />
<Route path="/posts" component={Posts} />
</Switch>
</div>
)
}
}
新:
const App = () => (
<div>
<Switch>
<Route exact path='/' component={Home} />
<Route path="/signin" component={RequireUnAuth(Signin)} />
<Route path="/signout" component={Signout} />
<Route path="/signup" component={Signup} />
<Route path="/posts" component={Posts} />
</Switch>
</div>
)
这是正在导出的应用程序,一旦我切换到一个简单的功能而不是基于组件的类,一切正常。我没有必要进行硬刷新,我可以点回来然后返回,使用this.props.history.push重定向工作等...
答案 1 :(得分:0)
确保安装了最新版本的react-router-redux(https://github.com/reactjs/react-router-redux),并支持React Router 4。在package.json中:
"react-router-redux": "next"
接下来,按照有关将历史中间件添加到redux商店的说明进行操作。链接:https://github.com/reactjs/react-router-redux#what-if-i-want-to-issue-navigation-events-via-redux-actions
一旦安装了正确的软件包和中间件,就需要将reducer导入索引缩减器,例如:
import {combineReducers} from 'redux';
import {routerReducer} from 'react-router-redux';
const IndexReducer = combineReducers({
router: routerReducer,
//...other reducers go here
});
export default IndexReducer;
然后,在您想要发送它的任何地方导入push方法(例如actions.js):
import {push} from 'react-router-redux';
然后像这样使用它:
dispatch(push('/your/route/here'));