React + TypeScriptでのコンポーネントのエラー
概要
現在、TypeScript + Reactで開発中ですが、下記の様なエラーが発生いたしました。
エラーの意味と該当箇所は恐らく分かっています。
具体的には、<Icon />
でundefinedをコンポーネントとして描画しようとしている為なのかなと…。
しかし、それを解決する方法が分かりません。
どなたか解決方法のご教授お願いいたします。
エラー内容
index.js:1 Warning: React.createElement: type is invalid -- expected a string (for built-in components) or a class/function (for composite components) but got: undefined. You likely forgot to export your component from the file it's defined in, or you might have mixed up default and named imports
該当のソースコード
TypeScript
1import React from 'react' 2import styled from 'styled-components' 3 4import { useSelector, useDispatch } from "react-redux" 5 6import * as actionTypes from "../utils/actionTypes" 7 8import { makeStyles } from '@material-ui/core/styles' 9import Snackbar from '@material-ui/core/Snackbar' 10import SnackbarContent from '@material-ui/core/SnackbarContent' 11import IconButton from '@material-ui/core/IconButton' 12import CloseIcon from '@material-ui/icons/Close' 13import CheckCircleIcon from '@material-ui/icons/CheckCircle' 14import WarningIcon from '@material-ui/icons/Warning' 15import ErrorIcon from '@material-ui/icons/Error' 16import InfoIcon from '@material-ui/icons/Info' 17 18interface iVariantIcon { 19 [key: string]: any, // シグネチャー 20} 21 22interface iClasses { 23 [key: string]: string, 24} 25 26const variantIcon: iVariantIcon = { 27 success: CheckCircleIcon, 28 warning: WarningIcon, 29 error: ErrorIcon, 30 info: InfoIcon, 31} 32 33const Notification: React.FC = () => { 34 const classes: iClasses = useStyles() 35 36 const dispatch = useDispatch() 37 const closeNotification = () => dispatch({ type: actionTypes.CLOSE_NOTIFICATION }) 38 39 const notificationOpen = useSelector((state: any) => state.NotificationReducer.notificationOpen) 40 const variant = useSelector((state: any) => state.NotificationReducer.variant) 41 const message = useSelector((state: any) => state.NotificationReducer.message) 42 43 const Icon = variantIcon[variant] 44 45 return ( 46 <Snackbar 47 anchorOrigin={{ 48 vertical: 'top', 49 horizontal: 'center', 50 }} 51 open={notificationOpen} 52 autoHideDuration={2000} 53 onClose={closeNotification} 54 > 55 <SnackbarContent 56 className={classes[variant]} 57 aria-describedby="client-snackbar" 58 message={ 59 <Span> 60 <Icon className={classes.icon} /> 61 {message} 62 </Span> 63 } 64 action={[ 65 <IconButton 66 key="close" 67 aria-label="Close" 68 color="inherit" 69 onClick={closeNotification} 70 > 71 <StyledCloseIcon /> 72 </IconButton> 73 ]} 74 /> 75 </Snackbar> 76 ) 77} 78 79const useStyles = makeStyles(() => ({ 80 success: { 81 backgroundColor: '#0074bf', 82 }, 83 error: { 84 backgroundColor: '#c93a40', 85 }, 86 info: { 87 backgroundColor: '#0074bf', 88 }, 89 warning: { 90 backgroundColor: '#c93a40', 91 }, 92 icon: { 93 fontSize: 20, 94 opacity: 0.9, 95 marginRight: 10 96 } 97})) 98 99const Span = styled.span` 100 display: 'flex', 101 alignItems: 'center', 102` 103 104const StyledCloseIcon = styled(CloseIcon)` 105 font-size: 20; 106` 107 108export default Notification
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。