-
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
solution #2131
base: master
Are you sure you want to change the base?
solution #2131
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/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.
const [newMovie, setNewMovie] = 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 obj to a variable, let's say InitialValues to be able to reuse it, for example in reset method
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.
isn't fixed. Create a variable outside the component and reuse it
const initialMovieState = {
title: '',
description: '',
imgUrl: '',
imdbUrl: '',
imdbId: ''
}
const [newMovie, setNewMovie] = useState({ | |
title: '', | |
description: '', | |
imgUrl: '', | |
imdbUrl: '', | |
imdbId: '', | |
}); | |
const [newMovie, setNewMovie] = useState(initialMovieState); |
src/components/NewMovie/NewMovie.tsx
Outdated
imdbId: '', | ||
}); | ||
|
||
const ifAllFieldsFilled = newMovie.title.trim() && newMovie.imgUrl.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.
let's rename this var to something like isAllFieldsFilled
And read this article about naming boolean variables)
@@ -23,6 +23,7 @@ export const TextField: React.FC<Props> = ({ | |||
placeholder = `Enter ${label}`, | |||
required = false, | |||
onChange = () => {}, | |||
// isValidUrl = () => {}, |
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 commented 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.
Approved)
Yet please apply that recommendation Anastasiia asked
const [newMovie, setNewMovie] = 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.
isn't fixed. Create a variable outside the component and reuse it
const initialMovieState = {
title: '',
description: '',
imgUrl: '',
imdbUrl: '',
imdbId: ''
}
const [newMovie, setNewMovie] = useState({ | |
title: '', | |
description: '', | |
imgUrl: '', | |
imdbUrl: '', | |
imdbId: '', | |
}); | |
const [newMovie, setNewMovie] = useState(initialMovieState); |
setNewMovie({ | ||
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.
setNewMovie({ | |
title: '', | |
description: '', | |
imgUrl: '', | |
imdbUrl: '', | |
imdbId: '', | |
}); | |
setNewMovie(initialMovieState); |
DEMO LINK