実現したいこと
input1つ1つにonChageの関数を書くのは冗長な気がするためhandlePasswordChange
とhandleEmailChange
を1つのonChage
の関数で書く方法を知りたい。
typescript
1import { useDispatch, useSelector } from 'react-redux'; 2import { RootState } from '../reducers/root_reducers'; 3import { loginAction } from '../actions/login_action'; 4 5 6function Login() { 7 const dispatch = useDispatch(); 8 const selector = useSelector((state: RootState) => state.signIn) 9 10 const handleEmailChange = (e) => { 11 const InputEmail = e.target.value; 12 dispatch(loginAction( 13 { email: InputEmail, password: selector.password } 14 )) 15 }; 16 17 const handlePasswordChange = (e) => { 18 const InputPassword = e.target.value; 19 dispatch(loginAction( 20 { email: selector.email, password: InputPassword } 21 )) 22 } 23 24 return ( 25 <> 26 <form action=""> 27 <input type="email" name="email" value={selector.email} onChange={handleEmailChange} autoFocus /> 28 <input type="password" name="password" value={selector.password} onChange={handlePasswordChange} /> 29 <input type="submit" value="Lgoin" /> 30 </form> 31 </> 32 ) 33} 34 35export default Login
あなたの回答
tips
プレビュー