嗨,我正在做一个应用程序,我有一个注册页面与表格,我注册了一个用户,并通过我的API与NodeJS通过我的POST路由发送数据.
我能够做到这一点,没有不便,但我知道,随着我的应用程序将增长和有几个功能,它会更好,如果我使用redux工具包.
这就是我正在做的事情.
import React, { useState } from 'react'
import axios from 'axios'
//Material UI
import {Grid, Container, Box, Button, FormControl, InputLabel, Input, FormHelperText } from '@mui/material'
function Register() {
const [ formData, setFormData ] = useState({
name: '',
email: '',
password: '',
password2: ''
})
const { name, email, password, password2 } = formData;
const onChange = e => setFormData({...formData, [e.target.name]: e.target.value})
const onSubmit = async (e) => {
e.preventDefault();
if(password !== password2){
console.log("Passwords do not match");
} else {
const newUser = {
name: formData.name,
email: formData.email,
password: formData.password
}
try {
const config = {
headers: {
'Content-Type': 'application/json'
}
}
const body = JSON.stringify(newUser);
const res = await axios.post('http://localhost:3000/api/users', body, config)
console.log(res.data)
} catch (error) {
console.error(error.response.data)
}
}
}
return (
<Container>
<Box p={4}>
<Grid container >
<form onSubmit={e => onSubmit(e)}>
<Grid item md={12}>
<FormControl>
<InputLabel htmlFor="name">Name</InputLabel>
<Input
type='text'
id='name'
name='name'
value={name}
onChange={e => onChange(e)}
/>
<FormHelperText>Insert your complete name</FormHelperText>
</FormControl>
</Grid>
<Grid item md={12}>
<FormControl>
<InputLabel htmlFor="email">Email</InputLabel>
<Input
id='email'
type='email'
name='email'
value={email}
onChange={e => onChange(e)}
/>
<FormHelperText>Insert your email</FormHelperText>
</FormControl>
</Grid>
<Grid item md={12}>
<FormControl>
<InputLabel htmlFor="password">Password</InputLabel>
<Input
id="password"
type='password'
name='password'
value={password}
onChange={e => onChange(e)}
/>
<FormHelperText>Insert your password (must have at least 6 characters)</FormHelperText>
</FormControl>
</Grid>
<Grid item md={12}>
<FormControl>
<InputLabel htmlFor="password2">Confirm Password</InputLabel>
<Input
id="password2"
type='password'
name='password2'
value={password2}
onChange={e => onChange(e)}
/>
<FormHelperText>Insert your password (must have at least 6 characters)</FormHelperText>
</FormControl>
</Grid>
<Grid item md={12}>
<Button type='submit' variant="contained" color="primary">
Register
</Button>
</Grid>
</form>
</Grid>
</Box>
</Container>
)
}
export default Register
我现在想try 并开始使用redux清理我的代码和管理状态,但我仍然对这个库感到非常困惑,我不知道如何继续.
我已经创建了一个store ,并将其传递给供应商,但我在理解切片方面遇到了困难.
这就是我到目前为止所拥有的:
import { createSlice } from '@reduxjs/toolkit'
const initialState = {
token: localStorage.getItem('token'),
isAuthenticated: null,
loading: true,
user: null
}
export const userSlice = createSlice({
name: 'user',
initialState,
reducers: {
},
})
// Action creators are generated for each case reducer function
export const { } = counterSlice.actions
export default counterSlice.reducer
因此,如果我通过表单发送的数据没有错误,我会得到一个令牌.我想让两个Reducer函数,一个用来注册我的用户,而不是把所有代码块放在我的组件中,另一个用我之前收到的令牌来验证用户.