score:1
You're using arrays to store your value and setter function where you're setting total
as the first value (index 0
) and setTotal
as the second value (index 1
).
The problem with this is when you write const [setTotal] = totalData;
you are actually grabbing the first value in the totalData
array (which is total
) and renaming it as setTotal
.
So now you're trying to call setTotal()
but you're effectively writing total()
which gives you that error.
Solution:
It would be a better developer experience to store these values as keys in an object so that you can destructure them the way you want to.
Change you context provider this:
<Context.Provider value={{ totalData: { total, setTotal } }}>
{props.children}
</Context.Provider>
Then use it like this:
const { totalData } = useContext(Context);
const { setTotal } = totalData;
Note:
If you really want to keep using arrays like you originally have been doing this, then you can destructure it like this:
const [_, setTotal] = totalData;
// Or
const setTotal = totalData[1];
This way you are grabbing the second item in your array (the set function), and simple ignoring the first (total
) value.
Source: stackoverflow.com
Related Query
- When setting state with React's use state hook using it's prev (or initial) value throws an error Type Error 0 is not a function
- Next.js with next-redux-wrapper state is being reset to initial value when navigating using Link
- Unable to update a list with react when its value is being changed using the hook useState function from React TypeError: map is not a function
- Setting initial state values when using react-select with Formik
- What typescript type do I use with useRef() hook when setting current manually?
- WebStorm inspector throws warning when setting initial React state with ES6
- Reading component state just after setting when using useState hook in react
- cant update context state when using hooks with a complex object to set providers value
- React: how to avoid re-rendering a component with a hook that returns always the same value but changes its inner state
- How to manage the asynchrounous nature of useState hook when setting a value and using it directly after
- How do I update state with a button click when initial state is set in an useEffect hook api call?
- React state is undefined when setting it with a hook
- Custom react hook with useState always resetting to its initial value
- How to change state value when input is checked with useState hook
- Having trouble with radio buttons forgetting its state when using React-Window
- How to type the initial state for react context when using useState as value
- How to stop infinite loop when setting initial state from API with useReducer (React Hooks)
- after setting the input field value to a state object with an empty value, now I can't type anything in the input field in the UI. i am using react js
- Keep URL and state when working with filter drop-downs using react hook
- How do I set an initial value for a text input when using a custom hook in react?
- Setting the initial value of useState with async function using contextAPI and React
- Problem with custom ContextProvider state when using custom hook for fetching data in multiple tables in React
- React useState() hook returns undefined when using option chained value as initial count
- state value does not change when using useEffect hook
- How to reset a result state to its initial value in react hook
- Setting state value coming from axios when using useState() and useEffect() in React
- Get specific state value when using custom Hooks ( trying to implement a form handling with hooks)
- Is there a way to use a variable as part of a value when setting state from props in ReactJS?
- State not updating when using React state hook within setInterval
- Getting an error "A non-serializable value was detected in the state" when using redux toolkit - but NOT with normal redux
More Query from same tag
- How to prevent inline functions from binding to old state values
- Enzyme not selecting an onClick for a component
- React how to apply style inside jsx
- React not finding Express server delete request. (MERN delete problem)
- How do I debug Axios frontend POST (React) to backend express POST? Console shows request as POST and status 200
- Redirect to Product Detail Page from Product List through JSON Data with ReactJS
- Redirect without locale in URL nextjs
- How do I make it so that <Input> only allows alphabet, whitespace and backspace?
- TypeScript: How to pass type information from parent to child?
- How to update array in immutable way
- How to pass a variables for refetchQueries in Apollo
- Return a word from a words array when the time is the one from the time array. Javascript - React
- Invalid response from getStaticProps with Next.js
- ReactJS+MaterialUI v1 automatic nested list padding
- Optimize javascript function code if ... else
- React Rails: Objects are not valid as a react child
- React: Image uploaded from node js backend is not rendering
- react-data-grid cannot implement row grouping - possible addons issue as well
- react router gives a url with a pad sign and a get parameter
- How do I achieve an average star with react and font awesome?
- Django Google Login with React Frontend
- React Router (Dom) v4 redirect to different route upon input enter key press
- Test Drop Down With React Testing Library
- How to convert a blob string to image in react
- Clear reactJs textarea after submit
- Sticky navigation bar -React
- Firebase UseEffect
- I cant get my state to update using hooks
- Infinite calendar remove selection handler
- Passing data between components into React (props)