Best practices for React JS developer part-2
ReactIn the previous article, we learned 5 areas or tips on Best practices for React JS developer.
In this article, we will learn more about areas or tips
- Object Destructuring.
- When pass value in props
- Tags close it self.
- Do not use underscore in any method name.
- Use alt in img tag.
Object distracting
You can use object distracting
Not Good
return (
<>
{user.firstName}
</>
)
Good
const { firstName } = user;
return (
<>
{firstName}
</>
)
When pass value in props
When pass the data between two component that time use camelCase for prop names.
Not Good
<Home
UserName="demo"
phone_number={9898989898}
/>
Good
<Home
userName="demo"
phoneNumber={9898989898}
/>
Tags close it self
Any component in not use children so closing it self
Not Good
<Home userName="demo"></Home>
Good
<Home
userName="demo"
/>
Do not use underscore in any method name
You don't need underscores in any React method.
Not Good
const _onClickHandler = () => {
// Your code
}
Good
const onClickHandler = () => {
// Your code
}
Use alt attribute in img tag
Do use alt attribute in the img tag. and don’t use picture or image name in your alt property.
Not Good
<img src="hello.png" />
Good
<img src="hello.png" alt="This is hero image" />