score:1
you cannot modify a state directly
you can create a copy, shuffle it and reassign the state in this way
const shuffle = () => {
setusedquestion(u => {
const data = [...u]
data.sort(() => math.random() - 0.5))
console.log("shuffled : " + data);
return data;
}
)
}
score:1
everytime you update a react state (eg. by calling setcurrentquestion) your component will rerender.
here, rerendering means it will execute the code of your function component again (function home()
).
in the body of function home()
you have this: let i = 0;
so i
will reset everytime you change a local state.
now what you probably want is to persist the current question index across multiple rerenders.
you could do nextquestion
like this:
// remove: const [currentquestion, setcurrentquestion] = usestate(1);
const [currentquestionindex, setcurrentquestionindex] = usestate(0);
const currentquestion = usedquestions[currentquestionindex];
const nextquestion = () => {
if (currentquestionindex < usedquestions.length) {
setcurrentquestionindex(currentquestionindex + 1);
} else {
setcurrentquestionindex(0);
}
};
you might have more tweaking to do but that should get you started
Source: stackoverflow.com
Related Query
- Use state in React causing value of const to break
- React - How to use the current state value as a variable so i can set as a index inside an array
- How to use a state value with map function to display repeat ui in react component
- Will React rerender if I use setState but the state value stay the same?
- React - How do I use a handleInputChange function to change a state value that is within a state value?
- How do I use React setState() to set the state to a specific value that is a constant?
- React use object value as state variable name
- React setState and use the value to update another state
- How can we use a value from component state while exporting in react
- use context value as initial state value - react hooks
- Axios request after state update in React (to use state value in url)
- React + Redux - Input onChange is very slow when typing in when the input have a value from the state
- Incrementing state value by one using React
- How do I use local state along with redux store state in the same react component?
- semantic ui react Setting dropdown value to state
- Does react re-render the component if it receives the same value in state
- Do not mutate state directly, Use setState() react/no-direct-mutation-state in React JS
- Get state value by a dynamic key in react
- React - State Update value only of Particular key
- How to use React Context with useState hook to share state from different components?
- How to assign a prop value to a state in react
- Why does React have to use setState for state update?
- React Native - TextInput - How to use value & defaultValue together
- How to add a new key value to react js state array?
- How to use setState() in React to blank/clear the value of an array
- React + Formik: Use value for nested object
- React Hook useCallback not updating State value
- How to use react ref to get value from html select element?
- How to use meta tags with dynamic value in react js?
- Should one use const when declaring an arrow function in React class
More Query from same tag
- local image won't load on canvas
- REACT REDUX Uncaught ReferenceError
- Sending data through axios post gives null data in mysql.Can anyone tell me what is the problem
- React Deleting checkbox item from list, strange behavior
- How to change background color when a variable is negative?
- How to change material-ui select value?
- The component sc-global has been created dynamically warning
- Redux reducers initializing same state key
- Image URL doesn't exist while using Sitecore with React JS?
- React router dom not rendering child component but url changed
- Webscraping from React web application after componentDidMount
- from react class to hooks change
- React - populate dropdown from array inside object and filter data
- document.execCommand ('copy') don't work in React
- Deleting an object inside another object in React
- React Router v4 routes with parameters not rendering/refreshing
- Changing height of div element by adding the number of items React.js
- Data formula to work out 3 scenarios from one value
- Isolating a function when data is mapped in react
- React bootstrap 4 modal window close button bad formatting
- textarea preview like stackoverflow have
- React Data Grid Filter on Component
- Material UI: Is it possible to put the makeStyle styles in a separate file?
- JavaScript - find out the source of the argument
- Sorting array of objects in MUI cards depending on social media
- React native does not give error on undefined variables
- Send props to an array of components
- how do you use and change innerHTML in react?
- Add and Remove event listeners in React with componentDidMount and componentWillUnmount
- How to place an element at the left and right side of overflowing div