React、Material-ui、Firebaseでチャットを作成中
•Material-uiを使用せず、要素をinputにして同じようにしたら読み込むことは出来た
•Material-uiを使用して同じように、name,email,passwordをStateで管理しvalueの値を読み込んでFirebaseに反映したい
•現在のコード(React)
SignUp
1import React, { useState } from 'react' 2import { Link } from 'react-router-dom' 3import { makeStyles } from '@material-ui/core/styles' 4import AppBar from '@material-ui/core/AppBar' 5import Toolbar from '@material-ui/core/Toolbar' 6import Typography from '@material-ui/core/Typography' 7import IconButton from '@material-ui/core/IconButton' 8import MenuIcon from '@material-ui/icons/Menu' 9import TextField from '@material-ui/core/TextField' 10import ExitToAppIcon from '@material-ui/icons/ExitToApp' 11import Button from '@material-ui/core/Button' 12 13import firebase from '../config/firebase' 14import styled from 'styled-components' 15 16const useStyles = makeStyles(theme => ({ 17 root: { 18 flexGrow: 1, 19 }, 20 menuButton: { 21 marginRight: theme.spacing(2), 22 }, 23})) 24 25const useSignInput = makeStyles(theme => ({ 26 root: { 27 '& > *': { 28 margin: theme.spacing(1), 29 width: '25ch', 30 }, 31 }, 32})) 33 34const useSubmit = makeStyles(theme => ({ 35 root: { 36 '& > *': { 37 margin: theme.spacing(1), 38 width: '25ch', 39 height: '1ch', 40 }, 41 }, 42})) 43 44const SignUp = () => { 45 const classes = useStyles() 46 const inputs = useSignInput() 47 const submit = useSubmit() 48 const [name, setName] = useState('') 49 const [email, setEmail] = useState('') 50 const [password, setPassword] = useState('') 51 52 const handleSubmit = e => { 53 e.preventDefault() 54 55 firebase 56 .auth() 57 .createUserWithEmailAndPassword(email, password) 58 .catch(err => { 59 console.log(err) 60 }) 61 } 62 63 return ( 64 <div className={classes.root}> 65 <AppBar position="static" color="secondary"> 66 <Toolbar variant="dense"> 67 <IconButton edge="start" className={classes.menuButton} color="inherit" aria-label="menu"> 68 <MenuIcon /> 69 </IconButton> 70 <Typography variant="h6" color="inherit"> 71 Man United Chat 72 </Typography> 73 </Toolbar> 74 <Link to="/login">Login</Link> 75 </AppBar> 76 77 <SignForm> 78 <ExitToAppIcon color="secondary" fontSize="large" /> 79 <SignTitle>Sign up</SignTitle> 80 81 <form onSubmit={handleSubmit} className={inputs.root} noValidate autoComplete="off"> 82 <TextField 83 value={name} 84 id="name" 85 name="name" 86 onChange={e => setName(e.target.value)} 87 label="Name *" 88 variant="outlined" 89 /> 90 </form> 91 92 <form onSubmit={handleSubmit} className={inputs.root} noValidate autoComplete="off"> 93 <TextField 94 value={email} 95 id="email" 96 name="email" 97 onChange={e => setEmail(e.target.value)} 98 label="E-mail *" 99 variant="outlined" 100 /> 101 </form> 102 103 <form onSubmit={handleSubmit} className={inputs.root} noValidate autoComplete="off"> 104 <TextField 105 value={password} 106 id="password" 107 name="password" 108 onChange={e => setPassword(e.target.value)} 109 label="Password *" 110 variant="outlined" 111 /> 112 </form> 113 114 <Button type="submit" className={submit.root} variant="contained" color="secondary"> 115 Sign In 116 </Button> 117 </SignForm> 118 </div> 119 ) 120} 121 122const SignForm = styled.div` 123 padding-top: 50px; 124 text-align: center; 125 background-color: #939597; 126 color: #fff; 127 height: 85vh; 128` 129 130const SignTitle = styled.h1` 131 margin: 0; 132` 133 134export default SignUp 135
firebase
1import * as firebase from 'firebase/app' 2import 'firebase/auth' 3 4// 認証情報 5const firebaseConfig = { 6 //認証情報ID 7} 8 9// 初期化 10firebase.initializeApp(firebaseConfig) 11 12export default firebase 13
やったこと
•Material-uiのform要素をFormControlで囲んでそこに、idやvalue、onChangeなどを記載したがダメ
•onSubmit={handleSubmit}を<SignForm>要素に記載したがダメ
お時間のある方、ご協力お願い致します。
あなたの回答
tips
プレビュー