score:1
const ShoppingList = () => {
const [items, setItems] = useState({
data: [
{ id: 1, name: 'Soda' },
{ id: 2, name: 'ice' },
],
});
const handleSort = () => {
const sortedItems = items.data.sort((a, b) => {
const nameA = a.name.toUpperCase();
const nameB = b.name.toUpperCase();
if (nameA < nameB) return -1;
if (nameA > nameB) return 1;
return 0;
});
setItems({
data: sortedItems,
});
};
return (
<>
<button onClick={() => handleSort()}>Sort by name</button>
<ul>
{items.data.map((el, i) => (
<li key={el.id}>
<span>{el.name} </span>
<button onClick={() => handleRemove(item.id)}>×</button>
</li>
))}
</ul>
</>
);
}
score:1
function is(x: any, y: any) {
return (
(x === y && (x !== 0 || 1 / x === 1 / y)) || (x !== x && y !== y) // eslint-disable-line no-self-compare
);
}
score:2
const sortByName = (a, b) => a.name.toUpperCase().localeCompare(b.name.toUpperCase());
const ShoppingList = () => {
const [items, setItems] = useState(() => [
{ id: 1, name: "Soda" },
{ id: 2, name: "ice" },
]);
const [sortOrder, setSortOrder] = useState("original");
const sortedItems = React.useMemo(() => {
switch (sortOrder) {
case "byName":
return [...items].sort(sortByName);
default:
return items;
}
}, [items, sortOrder]);
return (
<>
<button onClick={() => setSortOrder("byName")}>Sort by name</button>
<button onClick={() => setSortOrder("original")}>Sort in original order</button>
<ul>
{sortedItems.map((el, i) => (
<li key={el.id}>
<span>{el.name} </span>
<button>×</button>
</li>
))}
</ul>
</>
);
};
Source: stackoverflow.com
Related Query
- Not rendering while state is changed
- Component is not re rendering when state is changed in react
- React not rendering list after the state is changed
- React state changed not re rendering child
- Objects are not valid as a React child with state while rendering error
- React rendering problem in input box after fetching data in useEffect(). the state changed but textbox value not changing
- ReactJS component not rendering textarea with state variable
- React component not updating when store state has changed
- ReactJs: How to pass the initial state while rendering a component?
- Successful dispatch does not cause re-render even though state has been changed
- React memo keeps rendering when props have not changed
- ReactJS useEffect not rendering after dependency changed
- useReducer: dispatch causes rerender even if state is not changed
- React hooks - state in useState() is not reset when route is changed
- Conditional Rendering in React won't work, state not working properly?
- Nested map is not rendering the Redux State Correctly
- Component rerenders when state has not changed on the second click
- Rendering components in different containers while sharing state
- React setState does not set state during a while loop
- Map of React Components not re rendering on State Change
- React State change is not Rendering
- React is not updating when Redux state is changed below the first level
- Component does not rerender but redux state has changed via react hooks
- React-typing-animation is not re-rendered when state is changed
- Interesting Css rendering issue on zoom : browser renders a space gap between containers while zooming, occurs in every browser, but not in Firefox
- Updated state not rendering in child component
- Why am I not rendering my state to the screen?
- REACT + REDUX: on redux state change mapStateToProps updating state but view is not rendering as per new state
- Ant-Design Table not rendering when change state in mobx store
- State in react is not rendering to the screen
More Query from same tag
- Add functional component dynamical in react with out using class component
- Graphql query returning null for relational document, field missing from query but present in primsa admin
- Unable to prevent flashing unauthorized content in next.js before redirecting to a route
- In React / Redux and redux-thunk, what happens if fn()(dispatch) is called when fn is a thunk?
- increase react application performance
- using webpack ProvidePlugin in React storybook custom webpack config
- Passing data from child component to parent component error
- Updating state from an array with react useState hook
- Input onChange w/ React & TypeScript errors: jsx no-lambda no-bind
- How to se defaultValue for AtlasKit editor/editor-core?
- Convert Webpack syntax to browserify syntax
- Nested Object mutates in React
- How to Update This Reactjs Select
- How to populate the state after an action call in useEffect?
- Feather icon not displayed after using react-bootstrap-carousel
- React - setting up dynamic FormattedMessage - error "Messages must be statically"
- Server side rendering with loadable components not working
- Redux Action "Type alias 'Action' circularly references itself"
- Debounce gets wrong argument value on execution
- React Router and redux auth token double render
- passing props from functional component?
- React component is re-rendering items removed from state
- Can I set state property as a callback?
- Save recent searches react js
- componentDidMount not rendering everything
- React redux update state onClick function with multiple actions
- Access a Nested array from MongoDB collection
- React jsx attribute avoid duplicate attribute name
- How to catch axios api call error 401 in reactjs?
- React testing library - waiting for state update before testing component