-
Notifications
You must be signed in to change notification settings - Fork 2.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Develop #2122
base: master
Are you sure you want to change the base?
Develop #2122
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great work!
A user can create a film with spaces only, consider fixing it
src/components/NewMovie/NewMovie.tsx
Outdated
const [title, setTitle] = useState(''); | ||
const [description, setDescription] = useState(''); | ||
const [imgUrl, setImgUrl] = useState(''); | ||
const [imdbUrl, setImdbUrl] = useState(''); | ||
const [imdbId, setImdbId] = useState(''); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's not advisable to generate such a large number of states when we only have one instance (movie).
It'd be much better to utilize a single state with an object inside.
- Create a variable outside the Component
const initialMovieState = {
title: '',
description: '',
imgUrl: '',
imdbUrl: '',
imdbId: ''
}
- Use one state for movie fields
- Pass
initialMovieState
object when you invoke resetfunction
- Whenever you need to change value of any field you should pass two arguments
(key, value)
and then just change a value of a movie object by key. In this case you will reuse one handler instead of handlers for each input
Example:
const handleInputChange = (key, value) => {
setMovie(prevInputs => ({...prevInputs, [key]: value}))
}
- for reset function you will just pass initialMovieState
const [title, setTitle] = useState(''); | |
const [description, setDescription] = useState(''); | |
const [imgUrl, setImgUrl] = useState(''); | |
const [imdbUrl, setImdbUrl] = useState(''); | |
const [imdbId, setImdbId] = useState(''); | |
const [movie, setMovie] = useState(initialMovieState); |
src/components/NewMovie/NewMovie.tsx
Outdated
|
||
const handleSubmit = (event:React.FormEvent) => { | ||
event.preventDefault(); | ||
const trimmedTitle = title.trim() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lgtm
DEMO LINK