-
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
Solution #1886
base: master
Are you sure you want to change the base?
Solution #1886
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.
we can combine it in one useState
const [newMovie, setNewMovie] = useState({
title: '',
...
})
now we can also create only one handle function for all fields
const handleChange = (e: ...) => {
const {name, value} = e.target;
setMovie((prevMovie) => ({...prevMovie, [name]: value}))
}
don't forget to specify name attr to input in TextField
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.
I see you did a hard work) Let's improve it a little bit more
}) => { | ||
// generage a unique id once on component load | ||
const [id] = useState(() => `${name}-${getRandomDigits()}`); |
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.
what's the point of using state if u don't reassign it? let it be just const id = ...
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!
const [movies, setMovies] = useState<Movie[]>(moviesFromServer); | ||
|
||
const addMovie = (newMovie: Movie) => { | ||
setMovies([...movies, newMovie]); |
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.
setMovies([...movies, newMovie]); | |
setMovies(movies => [...movies, newMovie]); |
DEMO LINK