New answers tagged

2

There's no reason to use a HOC for this, it can be done more simply and clearly with a component: class List extends React.Component { state = { data: this.props.initialData, element: "" }; add = item => { this.setState(prev => ({ data: prev.data.concat(item) })); }; remove = keyToRemove => { this.setState(prev => ({ data:...


3

The code in componentDidUpdate looks a little odd to me. if(this.state.deck.length === 0){ You are checking if there aren't any cards, then drawing some cards. This works because you are initialising the deck within getPlayerCard. What I would do is put the deck initialisation within the startNewGame method instead. getDealerCard and getPlayerCard are ...


3

I've noticed you have a common bug that often occurs in Javascript code called a race condition. If uniqueIdCreatorHandler is called again before a previous call finishes there is a race condition created because state.ids is replaced at the end of the call. If both calls happen at the same time they each generate a unique copy of the original state.ids ...


0

function markTodoItemComplete(id) { const todoListClone = todoList.slice(); todoListClone[id - 1].complete = true; setTodoList(todoListClone); } This seems me not good practice, what you could have done is just getting id(key) of array and just set it to true. like function markTodoItemComplete(id) { todoListClone[id].complete = true; }...


1

Lifecycle hooks are functions that are invoked at different stages of a component. Here constructor() and componentDidMount() works different. From my point of view, state variables are initialized inside constructor() and api calls are done in componentDidMount() hook. we are not supposed to define function definitions inside lifecycle hooks. Your above ...


2

If the list never changes once loaded, the data would only ever need to be sorted one time (which ever you prefer as the default). Once sorted, you can just reverse the array as you currently are. Additionally, if your data comes from a database of sorts, you may not even need to sort on the client at all, you could return an already sorted resultset. So ...


1

If it works as expected, there's nothing wrong with it. However to improve the readability, I'd separate the setState call from actual sorting. Also be careful when you change the order state variable and when you use that variable to determine the sort order. orderUsers() { if (this.state.order) { return this.state.users.sort((a, b) => { if (...


2

Instead of resorting the list in state just store the preferred order in state. Then in render simply sort or reverse the order as needed to present the data. Be sure to use keys that don't change when the order is changed and it'll be quite performant.


2

Probably the biggest issue I see here is naming. In particular, isTrue is very vague - perhaps is isHovered would be better? And conventionally you use set... rather than handle... with useState so this should be setIsHovered. Similarly, I think you should rename setArg to setIconType. Also, I don't think you can pass 2 seperate functions as a prop. You ...


Top 50 recent answers are included