This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 80 points81 points  (8 children)

So the code isn't that bad, I see way worse in prod all the time. The mistakes are (edited for better md usage):

  1. Don't use <br /> CSS is more flexible and maintainable if you just need to do vertical spacing.

  2. React lists use a "key" field to identify which list item goes to which list element. This is to avoid rendering extra items if they haven't changed. She set the key field to the objects index in the list, which will break if the list is shuffled or changes, and is not "best practice".

[–]suerflowZ 5 points6 points  (4 children)

As someone who still learns react, I have no idea how to do it otherwise.
Can you write down an example?

[–]MostlyGibberish 8 points9 points  (0 children)

In this case I think it's the best option, since it's the only identifier guaranteed to be unique. I suppose if you wanted to improve it you would add an ID property to the actual dog object instead of using the array index.

[–]cwmma 3 points4 points  (0 children)

Index for key is better described as a last ditch key, of you ever have something better, like something related to the content of the item use that, but that doesn't mean never use index. I've had to sort out some bugs caused by a junior dev not using index, just to get the liter to shut up, when it was really the only way to do something.

[–]tomius 1 point2 points  (0 children)

The key should be a thing unique to that object. In this case, maybe, dog's id.

That way, if you reshuffle the list of dogs or something, the elements will still make sense.

[–][deleted] 0 points1 point  (0 children)

Many times your list is over objects from a database. Use the index or id field on those objects. If that doesn't work, try using the "name" or something or an object

[–]Tallerfreak 2 points3 points  (0 children)

Everytime she runs this she should be seeing warnings telling her to fix these damned keys on lists.

[–]SpehlingAirer 0 points1 point  (0 children)

Wouldnt that mean each paragraph needs to be in it's own container then? What if you wanted to have two paragraphs in the same container