-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
task done #877
base: master
Are you sure you want to change the base?
task done #877
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.
Good job. Some improvements required
src/App.tsx
Outdated
setComments(prevComments => prevComments | ||
.filter(comment => comment.id !== id)); | ||
postService.deleteComment(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.
What happens when postService.deleteComment - will catch an error?
src/components/NewCommentForm.tsx
Outdated
const [name, setName] = useState(''); | ||
const [email, setEmail] = useState(''); | ||
const [text, setText] = useState(''); | ||
const [nameError, setNameError] = useState(false); | ||
const [emailError, setEmailError] = useState(false); | ||
const [textError, setTextError] = useState(false); |
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.
My assumption it shoud be 2 states instead of 6
const [name, setName] = useState(''); | |
const [email, setEmail] = useState(''); | |
const [text, setText] = useState(''); | |
const [nameError, setNameError] = useState(false); | |
const [emailError, setEmailError] = useState(false); | |
const [textError, setTextError] = useState(false); | |
const [formFieds, setFormFieds] = useState({ | |
name: '', | |
email: '', | |
text: '', | |
}); | |
const [errorMessage, setErrorMessage] = 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.
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.
and about object, if i change only name, it will rerender all object. to my mind, its better to use 3 useStates.
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.
and about object, if i change only name, it will rerender all object. to my mind, its better to use 3 useStates.
FYI: You probably meant that we will change the entire object stored in the state, but that's not a problem. we do not render an object. we render the component (jsx that is written below), and re-create every variable (including) functions during the render. Having different states would be useful if you use memorization hooks with different variables in the dependency arrays.
src/components/NewCommentForm.tsx
Outdated
if (!name.trim()) { | ||
setNameError(true); | ||
} | ||
|
||
if (!email.trim()) { | ||
setEmailError(true); | ||
} | ||
|
||
if (!text.trim()) { | ||
setTextError(true); | ||
} | ||
|
||
if (!name.trim() || !email.trim() || !text.trim()) { | ||
return; | ||
} |
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 separate variables instead of 6 times calls trim()
src/components/NewCommentForm.tsx
Outdated
@@ -14,24 +98,33 @@ export const NewCommentForm: React.FC = () => { | |||
name="name" | |||
id="comment-author-name" | |||
placeholder="Name Surname" | |||
className="input is-danger" | |||
value={name} | |||
onChange={(event) => handleNameChange(event.target.value)} |
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.
Instead of additiona callback get target.value
in handleNameChange
Fix all similar cases
onChange={(event) => handleNameChange(event.target.value)} | |
onChange={handleNameChange} |
src/components/PostDetails.tsx
Outdated
return ( | ||
<article className="message is-small" data-cy="Comment"> | ||
<div className="message-header"> |
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.
Dont forget about key prop
src/components/PostsList.tsx
Outdated
return ( | ||
<tr data-cy="Post"> |
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.
Same here
src/components/UserSelector.tsx
Outdated
<a | ||
href={`#user-${user.id}`} | ||
onClick={() => handleSelect(user)} | ||
className={classNames( | ||
'dropdown-item', | ||
{ 'is-active': selectedUser?.id === user.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.
And here
src/App.tsx
Outdated
const [users, setUsers] = useState<User[]>([]); | ||
const [posts, setPosts] = useState<Post[]>([]); | ||
const [comments, setComments] = useState<Comment[]>([]); | ||
const [selectedUser, setSelectedUser] = useState<User | null>(null); | ||
const [selectedPost, setSelectedPost] = useState<Post | null>(null); | ||
const [isLoading, setIsLoading] = useState(false); | ||
const [isError, setIsError] = useState(false); | ||
const [isCmntsLoading, setIsCmntsLoading] = useState(false); | ||
const [isCmntsError, setIsCmntsError] = useState(false); | ||
const [isWriting, setIsWriting] = useState(false); |
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 are too many states and logic in App.tsx
try to move part of them to other components
src/components/NewCommentForm.tsx
Outdated
const handleSubmit = (event: React.FormEvent<HTMLFormElement>) => { | ||
event.preventDefault(); | ||
|
||
const nameValidate = !name.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.
const nameValidate = !name.trim; | |
const isNameValide = !!name.trim; |
for boolean values, it's better to use such names
src/components/NewCommentForm.tsx
Outdated
const [name, setName] = useState(''); | ||
const [email, setEmail] = useState(''); | ||
const [text, setText] = useState(''); | ||
const [nameError, setNameError] = useState(false); | ||
const [emailError, setEmailError] = useState(false); | ||
const [textError, setTextError] = useState(false); |
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.
and about object, if i change only name, it will rerender all object. to my mind, its better to use 3 useStates.
FYI: You probably meant that we will change the entire object stored in the state, but that's not a problem. we do not render an object. we render the component (jsx that is written below), and re-create every variable (including) functions during the render. Having different states would be useful if you use memorization hooks with different variables in the dependency arrays.
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 improve your code
src/components/UserSelector.tsx
Outdated
{users.map(user => ( | ||
<a |
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.
Use a destructuring for user
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.
Well done 🔥
https://pos1bl.github.io/react_dynamic-list-of-posts/