javascript - React/Redux 应用程序中组件的权限检查

标签 javascript reactjs ecmascript-6 redux react-redux

我正在尝试与构建 React 应用程序的团队合作,并试图找出创建“高阶”React 组件(包装另一个组件)的最佳方法,以结合 Redux 数据执行身份验证商店。

到目前为止,我的方法是创建一个模块,该模块由一个函数组成,该函数根据是否存在经过身份验证的用户返回一个新的 React 组件。

export default function auth(Component) {

    class Authenticated extends React.Component {

        // conditional logic

        render(){
            const isAuth = this.props.isAuthenticated;

            return (
                <div>
                    {isAuth ? <Component {...this.props} /> : null}
                </div>
            )
        }
    }

    ...

    return connect(mapStateToProps)(Authenticated);

}

这让我团队中的其他人可以轻松指定组件是否需要特定权限。

render() {
    return auth(<MyComponent />);
}

如果您要执行基于 Angular 色的检查,这种方法很有意义,因为您可能只有几个 Angular 色。在这种情况下,您可以调用 auth(<MyComponent />, admin) .

传递参数对于基于权限的检查变得笨拙。然而,在构建组件时在组件级别指定权限可能是可行的(以及在团队环境中可管理)。设置静态方法/属性似乎是一个不错的解决方案,但据我所知,es6 类导出为函数,不会显示可调用方法。

有没有办法访问导出的 React 组件的属性/方法,以便可以从包含组件访问它们?

最佳答案

onEnter 很棒,在某些情况下很有用。但是,以下是 onEnter 无法解决的一些常见身份验证和授权问题:

  • 根据 redux 存储数据 (there are some workarounds) 决定身份验证/授权
  • 如果商店更新(但不是 当前路线)

  • 如果子路由发生变化,则重新检查身份验证/授权 在 protected 路线下面

另一种方法是使用高阶组件。

您可以使用 Redux-auth-wrapper提供高阶组件,以便于阅读和应用组件的身份验证和授权约束。


  • 要获取子方法,您可以使用:refs、回调和来自 refs 的回调

  • 要获取子 Prop ,您可以使用:this.refs.child.props.some 或 compInstance.props.some

方法和 Prop 示例:

class Parent extends Component {
    constructor(props){
        super(props);
        this.checkChildMethod=this.checkChildMethod.bind(this);
        this.checkChildMethod2=this.checkChildMethod2.bind(this);
        this.checkChildMethod3=this.checkChildMethod3.bind(this);
    }
    checkChildMethod(){
        this.refs.child.someMethod();
        console.log(this.refs.child.props.test);
    }
    checkChildMethod2(){
        this._child2.someMethod();
        console.log(this._child2.props.test);
    }
    checkChildMethod3(){
        this._child3.someMethod();
        console.log(this._child3.props.test);
    }
    render(){
        return (
            <div>
                Parent
                <Child ref="child" test={"prop of child"}/>
                <ChildTwo ref={c=>this._child2=c} test={"prop of child2"}/>
                <ChildThree returnComp={c=>this._child3=c} test={"prop of child3"}/>
                <input type="button" value="Check method of child" onClick={this.checkChildMethod}/>
                <input type="button" value="Check method of childTwo" onClick={this.checkChildMethod2}/>
                <input type="button" value="Check method of childThree" onClick={this.checkChildMethod3}/>
            </div>
        );
    }
}

class Child extends Component {
    someMethod(){
        console.log('someMethod Child');
    }
    render(){
        return (<div>Child</div>);
    }
}
class ChildTwo extends Component {
    someMethod(){
        console.log('someMethod from ChildTwo');
    }
    render(){
        return (<div>Child</div>);
    }
}
class ChildThree extends Component {
    componentDidMount(){
        this.props.returnComp(this);
    }
    someMethod(){
        console.log('someMethod from ChildThree');
    }
    render(){
        return (<div>Child</div>);
    }
}

关于javascript - React/Redux 应用程序中组件的权限检查,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37982473/

相关文章:

javascript - 如果一个对象为空,则获取另一个值形式对象 Angular 6

javascript - 谷歌云端硬盘 SDK - JavaScript : How to list SHARED FILES (not shared with me)

reactjs - 如何使用带有 onChange 函数 REACT 的 useCallback Hook

javascript - 如何让 React 监听在 Jquery 中调用的点击事件

javascript : trim all properties of an object

javascript - React 中带有延迟子菜单 View 的菜单

javascript - 是否可以在 HTML5 中分别覆盖 Local Storage 和 Session Storage?

javascript - 给定时间和当前时间之间的进度条

javascript - 在轮询发生之前状态不会更新

javascript - 未应用常量文件中的设置颜色