无法设置未定义的属性“ props”

时间:2019-02-01 12:38:37

标签: javascript reactjs

我的代码有问题

  

TypeError:无法设置未定义的属性“ props”

我认为我做对了所有事情。我什至引用

react cannot set property of props of undefined

React-router: TypeError: Cannot set property 'props' of undefined

无法找出错误。

import React from 'react';
import {BrowserRouter as Router, Route, Link} from "react-router-dom";
import signUp from './signUp';
import signIn from './signIn';
import Users from './Users';
import AppBar from '@material-ui/core/AppBar';
import Toolbar from '@material-ui/core/Toolbar';
import Typography from '@material-ui/core/Typography';
import IconButton from '@material-ui/core/IconButton';
import MenuIcon from '@material-ui/icons/Menu';
import Button from '@material-ui/core/Button';
import {withStyles} from '@material-ui/core';
import Dashboard from './dashBoard';
import { connect } from 'react-redux';
import { createBrowserHistory } from 'history';
import PropTypes, { func, bool, string} from 'prop-types';

export const history = createBrowserHistory({forceRefresh: true});

const styles = {
    // This group of buttons will be aligned to the right

    rightToolbar: {
        color: '#fff',
        textDecoration: 'none',
        a: {
            color: '#fff'

        }
    },
    rightt: {
        marginLeft: 'auto',
        marginRight: 24
    },
    root: {
        flexGrow: 1
    },
    menuButton: {
        marginRight: 16,
        marginLeft: -12
    }
};



const logout = (e) => {
    e.preventDefault();
    localStorage.removeItem('JWT');

};


const Navbar = ({classes, props}) => (


    <Router history={history}>
        <div className={classes.root}>

            <AppBar position="static" className={classes.navbar}>
                <Toolbar>
                    <IconButton color="inherit" aria-label="Menu">
                        <MenuIcon/>
                    </IconButton>
                    <Typography variant="h6" color="inherit">
                        Eli App
                    </Typography>
                    <Typography classcolor="inherit" className={classes.rightt}>

                    {!props.token && ( 

                            <Button>
                            <Link to="/signUp" className={classes.rightToolbar} >
                                Sign Up
                            </Link>
                            </Button>



                    )}



                        <Button>
                            <Link to="/users" className={classes.rightToolbar}>
                              Users
                            </Link>
                        </Button>
                        <Button>
                            <Link to="/dashboard" className={classes.rightToolbar}>
                              Dashboard
                            </Link>
                        </Button>
                        <Button
                            onClick={logout}
                         >
                            <Link className={classes.rightToolbar} to={'/'}>
                                LogOut
                            </Link>
                        </Button>


                    </Typography>

                </Toolbar>
            </AppBar>

            <Route path="/signUp" component={signUp}/>
            <Route path="/signIn" component={signIn}/>
            <Route path="/users" component={Users}/>
            <Route path="/dashboard" component={Dashboard}/>
            <Route path="/signOut"/>
        </div>
    </Router>

);


const mapStateToProps = (state) => ({
    token: state.user.getToken
})

const mapDispatchToProps = (dispatch) => ({
    //   logIn: (user) => dispatch(logIn(user))

});

Navbar.propTypes = {
    token:PropTypes.string,

}

// export default withStyles(styles)(Navbar);
export default connect(mapStateToProps, mapDispatchToProps)(withStyles(styles))(Navbar);

已更新

enter image description here

2 个答案:

答案 0 :(得分:2)

您正在函数参数中解构的值已经是您的道具,如果您想访问token,则可以执行以下操作:

const Navbar = ({classes, token}) => ( //classes and token are INSIDE your props

在渲染功能中:

{!token && ( 

这似乎是这个问题也可能来自您的export

export default connect(mapStateToProps, mapDispatchToProps)(withStyles(styles))(Navbar);

您应该使用compose来同时使用多个HOC:

import { compose } from 'redux'

//....

export default compose(connect(mapStateToProps, mapDispatchToProps), withStyles(styles))(Navbar);

您还可以使用与终极版无状态的功能时,会遇到一些意外的行为,我建议使用也从@Shalini Sentiya的解决方案。

答案 1 :(得分:-1)

    Try to using the navbar react class instead of the function

    class Navbar extends Component {
      constructor(props){
        super(props);
      }
      render() {
        const { token } = this.props;
        return (
          <Router history={history}>
            // your code
            {!token && ( 
             <Button>
               <Link to="/signUp" className={classes.rightToolbar} >
                 Sign Up
               </Link>
             </Button>
           )} 
           // your code
         </Router>
        );
      }
    }
    const mapStateToProps = (state) => ({
        token: state.user.getToken
    })

    const mapDispatchToProps = (dispatch) => ({
        //   logIn: (user) => dispatch(logIn(user))

    });

    Navbar.propTypes = {
        token:PropTypes.string,

    }

    // export default withStyles(styles)(Navbar);
    export default connect(mapStateToProps, mapDispatchToProps)(withStyles(styles))(Navbar);