score:0
Accepted answer
by simplifying your code a little bit (mostly just removing the sorts function from the body of your code and cleaning up the useeffect hook), i was able to get it to work
import { usestate, useeffect } from "react";
function sorts(firstarr, secondarr) {
let c = [...firstarr, ...secondarr];
return c.sort();
}
export default function app() {
const [sorted, setsorted] = usestate([]);
const a = [1, 3, 5];
const b = [2, 4, 6];
useeffect(() => {
let sortedarr = sorts(a, b);
setsorted([...sortedarr]);
}, []);
return (
<div classname="app">
<h2>sorted array below</h2>
<p>{json.stringify(sorted)}</p>
</div>
);
}
score:1
i've tried to replicate the error in a codesandbox, the only thing causing an error, was a missing paranthesis. however your code was really hacky and unnecessary long. this is cleaner and works: https://codesandbox.io/s/flamboyant-pasteur-c9c83?file=/src/app.js
Source: stackoverflow.com
Related Query
- How to clone an array into useState hook
- How to change/add value of a certain row of an array using useState Hook in React
- How to use useState hook for a randomizing array problem(React useState)?
- How to Iteratively Store Multiple API Calls into a useState Array
- How to update an array using useState Hook
- React useState - how to put array of objects into state
- How to return a string from an array using React.js useState hook
- how to add object into array with hook state in react js
- How to pass data into useState using useEffect - next.js ( swr hook for fetching and cache )
- How to filter useState hook Array and push & pull data to that array?
- How to asynchronously update React useState hook array
- How to use array in UseState hook in react and how to update it dynamically
- How to use useState hook with array state for children that call the setter function without infinite rendering loop?
- How do I define the type that the useState hook is expecting if I want to store an array of objects?
- How do you destructure a React useState hook into a namespace?
- How to read the value of updated array while using useState hook in react?
- How to create nested array of objects using useState react hook
- How to update the nested array using useState Hook in React
- How to add/update the array department using the useState in react hook
- Creating a work hours component - How to useState with multiple values going into an array
- how to submit the form data into an array using useReducer hook & i have to get the data and to display it?
- How get multiple values from checkbox list into state using React useState hook
- Insert an array into state using useState hook in React
- How to use callback with useState hook in react
- How to set initial state for useState Hook in jest and enzyme?
- How can I insert into React's state array with setState?
- Displaying an array received from Usestate hook with Typescript
- How to prevent race conditions with react useState hook
- React efficiently update object in array with useState hook
- How to mock/spy useState hook in jest?
More Query from same tag
- changing singleinput code to dynamically add more input fields with same features in react
- i18n for array elements of react component
- Cross origin read blocking React - Nodejs
- Use object included from global <script> tag inside React component
- How to make image size dependent on the parents font size
- Next.js locale files are getting 404 because of setting basePath?
- reactjs, json, image loading
- How to redirect to from /login to elsewhere if user is signed-in using React router
- How to get updated state from react parent component
- Property 'isMounted' does not exist on type 'ErrorBoundary'
- How to create an excel file in react js with custom headings and styles while doing export to excel?
- How to retrieve the parameter after a hash (#) with React Router and useParams hook?
- How to get a single data element from the database with Redux and React?
- After using react-router the onClick event is not firing the function in first time and works fine after that
- Solve Jest test error from Webpack plugin
- React JS React Query's useMutation: Unable to retrieve the response from the server within the onError callback
- Trying to install react js in windows 10.but i got this error every time
- How to make a custom debounce hook works with a useCallback?
- I am trying to connect Lottery Contract to React Application using Metamask, But when I call manager() method on the contract I get an empty array
- Using feather icons with variables in ReactJs
- react-beautiful-dnd: Cannot find drag handle element inside of Draggable
- React: why useState magically React.createElement when used with Component?
- DatepickerInput defaultValue is not working
- Electron + ReactJS + Material-UI Rendering Glitches
- How to pass a JSON data from React to Django using POST request?
- How to make transition in React to display new HTML content based on updated state?
- How to mirror the state from switch button with ReactJS?
- React Chartjs 2 display axes above datasets backgroundColor
- Array FormData can't be read in ASP.NET
- Cannot destructure property 'chat' of '_ref' as it is undefined (firebase & react)