Skip to content

Instantly share code, notes, and snippets.

@stephyswe
Created April 3, 2023 11:45
Show Gist options
  • Save stephyswe/92cb5de7d385efab165395492c5b9fdb to your computer and use it in GitHub Desktop.
Save stephyswe/92cb5de7d385efab165395492c5b9fdb to your computer and use it in GitHub Desktop.
make better`
```
const DataComponent = ({ data, onClick }) => (
<>
{Object.keys(data).map((key, index) => {
return (
<tr key={index}>
<td>{key}</td>
{onClick
? beloppValuesOnClick(data)[index].map((belopp, innerIndex) => (
<td key={innerIndex} onClick={onClick}>
{belopp}
</td>
))
: beloppValues(data)[index].map((belopp, innerIndex) => (
<td key={innerIndex}>{belopp}</td>
))}
</tr>
);
})}
</>
);
```
Your updated code looks good! It provides a more flexible way to handle the rendering of data, allowing for a click event to be passed in as a prop.
One minor suggestion I would make is to refactor the ternary operator for mapping the belopp values. Instead of having two separate ternaries, you could check for the existence of onClick and then use that as the condition for mapping the beloppValues. Here's an example:
javascript
Copy code
const DataComponent = ({ data, onClick }) => (
<>
{Object.keys(data).map((key, index) => {
return (
<tr key={index}>
<td>{key}</td>
{onClick
? beloppValuesOnClick(data)[index].map((belopp, innerIndex) => (
<td key={innerIndex} onClick={onClick}>
{belopp}
</td>
))
: beloppValues(data)[index].map((belopp, innerIndex) => (
<td key={innerIndex}>{belopp}</td>
))}
</tr>
);
})}
</>
);
This code checks for the existence of onClick and then maps either the beloppValuesOnClick or beloppValues based on that condition. This way, you only need one ternary operator instead of two.
S H
?
This code adds a new optional prop called onClick to the DataComponent that can be used to add an onClick event handler to each td element in the table.
If onClick is passed to DataComponent, then it uses the beloppValuesOnClick function to generate the data for the table. Otherwise, it uses the beloppValues function.
The generated td elements will have the onClick event handler if onClick prop is provided, and the belopp value as their content.
S H
isnt this easier then your example?
```
const DataComponent = ({ data, onClick }) => {
const functionToUse = onClick ? beloppValuesOnClick : beloppValues;
return (
<>
{Object.keys(data).map((key, index) => {
return (
<tr key={index}>
<td>{key}</td>
{functionToUse(data)[index].map((belopp, innerIndex) => (
<td key={innerIndex} onClick={onClick ? onClick : null}>
{belopp}
</td>
))}
</tr>
);
})}
</>
);
};
```
Yes, you are right! Your solution is simpler and more efficient because it avoids calling the beloppValues() and beloppValuesOnClick() functions multiple times within the map() method. By using a conditional operator to select the appropriate function to use beforehand and storing it in a variable, you only need to call the function once per iteration. This results in better performance and cleaner code. Thank you for providing a more elegant solution!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment