-
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
movies list add form v1 #1889
base: master
Are you sure you want to change the base?
movies list add form v1 #1889
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.
src/App.tsx
Outdated
imgUrl: string; | ||
imdbUrl: string; imdbId: string; }) => { | ||
setMovies(prevState => [...prevState, movie]); | ||
}} |
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.
Consider moving type definitions outside JSX
src/components/NewMovie/NewMovie.tsx
Outdated
}); | ||
|
||
// [setTitle, setDescription, setImgUrl, setImdbUrl, setImdbId] | ||
// .forEach(func => func('')); |
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.
Remove unused code
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.
export const App = () => { | ||
const [movies, setMovies] = useState([...moviesFromServer]); |
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.
For what this spread operator, react created new array each render, and first value it's just default value which don't change moviesFromServer variable
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.
Combine it into 1 state { title, description, ..... }
@@ -1,5 +1,6 @@ | |||
import classNames from 'classnames'; | |||
import React, { useState } from 'react'; | |||
// import { Movie } from '../../types/Movie'; |
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.
Remove comment
src/components/NewMovie/NewMovie.tsx
Outdated
const [movieDef, setMovieDef] = useState({ | ||
title: '', | ||
description: '', | ||
imgUrl: '', | ||
imdbUrl: '', | ||
imdbId: '', | ||
}); |
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.
move this object to a variable (outside NewMovie component)
const initialMovieState = {
title: '',
description: '',
imgUrl: '',
imdbUrl: '',
imdbId: '',
}
const [movieDef, setMovieDef] = useState({ | |
title: '', | |
description: '', | |
imgUrl: '', | |
imdbUrl: '', | |
imdbId: '', | |
}); | |
const [movieDef, setMovieDef] = useState(initialMovieState); |
src/components/NewMovie/NewMovie.tsx
Outdated
setMovieDef({ | ||
title: '', | ||
description: '', | ||
imgUrl: '', | ||
imdbUrl: '', | ||
imdbId: '', | ||
}); |
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.
create a function reset
and move this logic there. By the way, you can just pass initialMovieState
const handleSubmit = (event: React.FormEvent) => { | ||
event.preventDefault(); | ||
setCount(prevState => prevState + 1); | ||
|
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.
before adding a movie you should validate values. Right now I can just press space
for all form fields and submit a form
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