Skip to main content

good practice - react project - array - switch - update object of array

 

setListing((prevState: any) => {
const todos = [...prevState.listingTodos]
const newTodos = todos.map(todo => {
return {
...todo,
expanded: todo.tabValue === todoValue,
}
})

return {
...prevState,
listingLoading: false,
listingTodos: newTodos,
}
})
// bad practice
const array = [ [ ['Shoaib Mehedi'] ] ]
array.forEach((firstArr) =>{
firstArr.forEach((secondArr) => {
secondArr.forEach((element) => {
console.log(element);
})
})
})
// good practice
const array = [ [ ['Shoaib Mehedi'] ] ]
const getValuesOfNestedArray = (element) => {
if(Array.isArray(element)){
return getValuesOfNestedArray(element[0])
}
return element
}
getValuesOfNestedArray(array)
const {role} = user const components = { ADMIN: AdminUser, EMPLOYEE: EmployeeUser, USER: NormalUser}; const Component = components[role]; return <Componenent />;


Don't Define a Function Inside Render


return (
<button onClick={() => dispatch(ACTION_TO_SEND_DATA)}> // NOTICE HERE
This is a bad example
</button>
)
const submitData = () => dispatch(ACTION_TO_SEND_DATA)
return (
<button onClick={submitData}>
This is a good example
</button>
)

Comments

Popular posts from this blog

Online tool to remove background of images

https://www.remove.bg/upload

React way to use D3 - ( Python as API )

The Inspiration Andmap is trying to build new BI product, the tech stacks would be D3 + ReactJS as data visualization frontend, Web API, and Python provides data. As with most new projects, it didn’t take long to figure out the major roadblocks. D3.js is a JavaScript library for manipulating documents based on data. D3 helps you bring data to life using HTML, SVG, and CSS. D3’s emphasis on web standards gives you the full capabilities of modern browsers without tying yourself to a proprietary framework, combining powerful visualization components and a data-driven approach to DOM manipulation. In one of our initial discovery sessions, we realized that our application needs to be able to adapt D3 library in ReactJS way. More than that, we prefer utilizing new feature of React - Hooks. Finally, we build  component for each chart and Helpers file for drawing each chart. Here is sample for map chart: index.js: map.js - map chart container - using useEf...