Issue Encounter
- I am currently encountering the error message
I have attempted to resolve it without success so far.'user' does not exist on type 'DefaultRootState'.
Here is the link to my GitHub repository.
Error Details
C:/Users/taiga/Github/Typescript/typescript-admin/admin/src/secure/components/Nav.tsx
TypeScript error in C:/Users/taiga/Github/Typescript/typescript-admin/admin/src/secure/components/Nav.tsx(44,50):
Property 'user' does not exist on type 'DefaultRootState'. TS2339
File: Nav.tsx
class Nav extends Component<{ user: User }> {
state = {
redirect: false
};
handleClick = async () => {
await axios.post('logout', {});
this.setState({
redirect: true
});
};
render() {
if (this.state.redirect) {
return <Redirect to={'/login'} />;
}
return (
<nav className="navbar navbar-dark sticky-top bg-dark flex-md-nowrap p-0 shadow">
<ul className="my-2 my-md-0 mr-md-3">
<Link to={'/profile'} className="p-2 text-white">
{this.props.user.name}
</Link>
</ul>
</nav>
);
}
}
export default connect((state) => ({ user: state.user }))(Nav);
User Class Definition (user.ts)
mport { Role } from './role';
export class User {
id: number;
first_name: string;
last_name: string;
email: string;
role: Role;
permissions: string[];
constructor(
id = 0,
first_name = '',
last_name = '',
email = '',
role = new Role(),
permissions: string[] = []
) {
this.id = id;
this.first_name = first_name;
this.last_name = last_name;
this.email = email;
this.role = role;
this.permissions = permissions;
}
Redux Reducer for Setting User (setUserReducer.ts)
import { User } from '../../classes/user';
const setUserReducer = (
state = { user: new User() },
action: { type: string; user: User }
) => {
switch (action.type) {
case 'SET_USER':
return {
...state,
user: action.user
};
default:
return state;
}
};
export default setUserReducer;