小编Pau*_*aul的帖子

React路由器不在服务器上重定向

我有一个高阶组件,检查用户是否经过身份验证,如果没有,将重定向到不同的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)

javascript reactjs react-router

6
推荐指数
1
解决办法
496
查看次数

第二次操作调用时React/Redux State为空

我仍然是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)

javascript node.js reactjs redux react-redux

5
推荐指数
1
解决办法
1360
查看次数

标签 统计

javascript ×2

reactjs ×2

node.js ×1

react-redux ×1

react-router ×1

redux ×1