-
Notifications
You must be signed in to change notification settings - Fork 2.5k
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
Add solution #1880
base: master
Are you sure you want to change the base?
Add solution #1880
Conversation
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}))
}
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 resetForm = () => { | ||
setTitle(''); | ||
setDescription(''); | ||
setImgUrl(''); | ||
setImdbUrl(''); | ||
setImdbId(''); | ||
}; |
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.
const resetForm = () => { | |
setTitle(''); | |
setDescription(''); | |
setImgUrl(''); | |
setImdbUrl(''); | |
setImdbId(''); | |
}; | |
const resetForm = () => { | |
setMovie(initialMovieState); | |
}; |
src/components/NewMovie/NewMovie.tsx
Outdated
resetForm(); | ||
}; | ||
|
||
const isSubmitDisabled = !(title |
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.
Good job
DEMO LINK