showing results for - "react show view based on role permission"
Alen
27 Jan 2017
1import PropTypes from 'prop-types';
2import { connect } from 'react-redux';
3
4const ShowForPermissionComponent = (props) => {
5    const couldShow = props.userPermissions.includes(props.permission);
6    return couldShow ? props.children : null;
7};
8
9ShowForPermissionComponent.propTypes = {
10    permission: PropTypes.string.isRequired,
11    userPermissions: PropTypes.array.isRequired
12};
13
14
15const mapStateToProps = state => ({
16    userPermissions: state.user.permission //<--- here you will get permissions for your user from Redux store
17});
18
19export const ShowForPermission = connect(mapStateToProps)(ShowForPermissionComponent);
20
Emmanuel
02 Sep 2017
1import React from 'react';
2import { ShowForPermission } from './ShowForPermission';
3
4cons MyComponent = props => {
5   return (
6        <div>
7            <ShowForPermission permission="DELETE">
8                <button>Delete</button>
9            </ShowForPermission>
10        </div>
11   );
12}
13
14