I have a problem with reseting my Form
after I submit it. I tried to do something like
document.getElementById("formularz").reset()
, but it doesn’t work, neither does doing in the end of handleOnSubmit
things like: event.target.title=""
. It does reset the fields, but when I start writing new data, suddenly in each input there are shown data from previous submit.
My component:
JavaScript
x
153
153
1
import {Form, Button} from 'react-bootstrap'
2
import {useState, useRef} from "react"
3
import {AiOutlineWarning} from 'react-icons/ai'
4
function BooksForm(props) {
5
const axios = require('axios')
6
const [book,setBook] = useState({
7
title: props.title ? props.title : '',
8
author: props.author ? props.author : '',
9
genre: props.genre ? props.genre : '',
10
release_date: props.release_date ? props.release_date : '',
11
description: props.description ? props.description : '',
12
image_url: props.image_url ? props.image_url: ''
13
14
15
16
})
17
18
const [errorMessage, setErrorMessage] = useState('')
19
20
const { title,
21
author,
22
genre,
23
release_date,
24
description,
25
image_url,
26
rating_count} = book;
27
28
const handleOnSubmit = (event) => {
29
event.preventDefault();
30
const values = [
31
title,
32
author,
33
genre,
34
release_date,
35
description,
36
image_url
37
];
38
let errorMessage = '';
39
40
const allFieldsFilled = values.every((field) => {
41
const value = `${field}`.trim();
42
return value !== '' && value !== '0' && value !== undefined;
43
});
44
45
if (allFieldsFilled) {
46
if (new Date(release_date) > new Date()) {
47
errorMessage='Data nie może być starsza niż dzisiejsza'
48
setErrorMessage(errorMessage)
49
}
50
else {
51
52
const book = {
53
54
title,
55
author,
56
genre,
57
release_date,
58
description,
59
image_url
60
61
};
62
props.handleOnSubmit(book);
63
64
document.getElementById("formularz").reset()
65
}
66
} else {
67
errorMessage = 'Please fill out all the fields.';
68
}
69
setErrorMessage(errorMessage);
70
};
71
72
73
74
const handleInputChange = (e) => {
75
const name = e.target.name
76
const value = e.target.value
77
setBook((prev) => ({prev, [name]: value}));
78
79
}
80
81
const handleOnEdit = (event) => {
82
console.log(book)
83
event.preventDefault()
84
axios.put('http://localhost:5000/api/book/'+props.id,book)
85
props.onEdit()
86
87
}
88
89
90
91
92
return (
93
<div className="form">
94
{errorMessage && <p className="errorMsg"><AiOutlineWarning />{errorMessage}</p>}
95
96
97
98
<Form>
99
<Form.Group controlId="nazwa">
100
<Form.Label>Wprowadź tytuł książki</Form.Label>
101
<Form.Control className="control-input" type="text" name="title" value={title} placeholder="Tu wpisz tytuł" onChange={handleInputChange} />
102
</Form.Group>
103
104
<Form.Group controlId="autor">
105
<Form.Label>Wprowadź autora książki</Form.Label>
106
<Form.Control className="control-input" type="text" name="author" value={author} placeholder="Tu wpisz autora" onChange={handleInputChange} />
107
</Form.Group>
108
109
<Form.Group controlId="genre">
110
<Form.Label>Wprowadź gatunek książki</Form.Label>
111
<Form.Control className="control-input" type="text" name="genre" value={genre} placeholder="Tu wpisz gatunek" onChange={handleInputChange} />
112
</Form.Group>
113
114
<Form.Group controlId="releaseDate">
115
<Form.Label>Wprowadź datę powstania książki</Form.Label>
116
<Form.Control className="control-input" type="date" name="release_date" value={release_date} placeholder="Tu wpisz datę powstania" onChange={handleInputChange} />
117
</Form.Group>
118
119
<Form.Group controlId="description">
120
<Form.Label>Wprowadź opis książki</Form.Label>
121
<Form.Control className="control-input" type="text" name="description" value={description} placeholder="Tu daj opis" size="30" onChange={handleInputChange} />
122
</Form.Group>
123
124
<Form.Group controlId="imageurl">
125
<Form.Label>Wprowadź adres URL do zdjęcia książki</Form.Label>
126
<Form.Control className="control-input" type="text" name="image_url" defaultValue={image_url} placeholder="Tu wprowadź adres URL do zdjęcia" size="30" onChange={handleInputChange} />
127
</Form.Group>
128
129
<Form.Group controlId="rating">
130
<Form.Label>Wprowadź ocenę książki</Form.Label>
131
<Form.Control className="control-input" type="number" name="rating_count" defaultValue={rating_count} placeholder="Tu wprowadź ocenę książki" size="30" onChange={handleInputChange} />
132
</Form.Group>
133
134
<Button type="submit" variant="primary" className="submitButton">Submit</Button>
135
136
137
138
139
140
141
</Form>
142
143
144
145
</div>
146
)
147
148
149
150
}
151
152
export default BooksForm;
153
Advertisement
Answer
JavaScript
1
6
1
setBook({
2
title: '',
3
author: '',
4
//rest of properties should look like that
5
})
6
Your form inputs are controlled by your state (as you put each input value to reflect the state property of the same name). In order for your form to reset, you need to set the state with a new object, structured as your current state – only with empty strings as values.