我有一个高阶组件,检查用户是否经过身份验证,如果没有,将重定向到不同的URL.
它是一个同构的应用程序,这适用于客户端,但如果我关闭JS,服务器不会重定向.
if (!this.props.authenticated) {
this.context.router.push('/');
}
Run Code Online (Sandbox Code Playgroud)
我可以在服务器上点击这个语句并this.context.router
返回,但没有任何反应.
完整组件:
import React, { Component, PropTypes } from 'react';
import { connect } from 'react-redux';
export default function (ComposedComponent) {
class Authentication extends Component {
static contextTypes = {
router: React.PropTypes.object
}
static propTypes = {
authenticated: PropTypes.bool
}
componentWillMount() {
if (!this.props.authenticated) {
this.context.router.push('/');
}
}
componentWillUpdate(nextProps) {
if (!nextProps.authenticated) {
this.context.router.push('/');
}
}
render() {
return <ComposedComponent {...this.props} />;
}
}
function mapStateToProps(state) {
return { authenticated: state.auth.authenticated …
Run Code Online (Sandbox Code Playgroud) 我仍然是React/Redux的新手,如果这是一个简单的问题我很抱歉,但我还没有找到解决方案.
我有两个动作:
// DESTINATIONS
// ==============================================
export const DESTINATION_REQUEST = 'DESTINATION_REQUEST';
export const DESTINATION_SUCCESS = 'DESTINATION_SUCCESS';
export const DESTINATION_FAILURE = 'DESTINATION_FAILURE';
export function loadDestination (params, query) {
const state = params.state ? `/${params.state}` : '';
const region = params.region ? `/${params.region}` : '';
const area = params.area ? `/${params.area}` : '';
return (dispatch) => {
return api('location', {url: `/accommodation${state}${region}${area}`}).then((response) => {
const destination = formatDestinationData(response);
dispatch({
type: DESTINATION_SUCCESS,
destination
});
});
};
}
// PROPERTIES
// ==============================================
export const PROPERTIES_REQUEST …
Run Code Online (Sandbox Code Playgroud)