score:1
React is called react because it re-renders when it reacts to something that changed.
For that to happen, the react components need to know, somehow, that something changed.
In the code example you show, the totalCount
(which global access provided by exported function getTotalCount
) is independent from react component tree, so that data can change without react knowing it.
So, the question is: how to make your react component aware of those changes?
A few mechanisms exist, here are a few of them:
Use a global state manager like redux, with redux actions and reducer to mutate the state and
useSelector
in the component that will make them aware of any change, and rerenderReact context which will hold the cart state. That context can export both that data, and th function that will mutate the data, so they can be shared between components that have access to that context provider.
Your context could be used in the component that way:
const cartContext = useContext(CartContext)
const { cartState, addToCart, removeFromCart } = cartContext
const { totalCount, inTheCart } = cartState
[...somwhere later...]
<ul><li onClick={() => removeFromCart(index)}>{inTheCart[index].name}</li></ul>
<footer>{`${totalCount} items`}</footer>
I let you build the context object around your existing functions ;)
score:1
A simple solution is to lift the state up. Create the state in your layout file from where Header
and Cart
can access the count
as Props
. useEffect
will be invoked whenever there is a change in the state
or props
of a component.
Source: stackoverflow.com
Related Query
- In React useEffect does not update after the value has changed from other component
- In React useEffect doesn't update after the value has changed from other component
- 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
- How to instantly update react state after replacing the value from array
- React: How does React make sure that useEffect is called after the browser has had a chance to paint?
- Assignments to the 'timeInterval' variable from inside React Hook useEffect will be lost after each render
- Conditionally check value from context and then navigate after login in the same function call in React
- How to pass the props value from the other sibling components in React JS
- React useEffect dependent on value which is initialized after api has been called
- React - how to update state from the prop of other library
- How to update the React State only after a return from API called is completed
- Read the setState value immediately after setting in useEffect React
- How to update a useState which has an array, inside this array I have objects this objects will update when the input value will change in react js
- How can I update the value from the database in React
- React test: after update input the props value still not change
- Test the changed state after useEffect has runned
- How to fetch from another API after useState has finished setting the value
- get the value from a text input after it is updated in react function
- React - I am passing state from a child to a parent component but the state value is off by one update cycle
- Assignments to the '[x]' variable from inside React Hook useEffect will be lost after each render
- How to Remove the form value After Submit in React from below code?
- How to update several states at the same time if their value depend on each other with react hooks
- How to pass a dynamic value from one component to the other in React
- calling the Redux-Thunk Action after Component Mount by using useEffect. but got issue : React Hook useEffect has missing dependencies
- React update the parents status from one child and pass the updated value to a new child
- How can I get the object which has been changed and also the new changed value when Selecting options from the drop down
- Cant Update The state after getting data from the input React js
- React rendering problem in input box after fetching data in useEffect(). the state changed but textbox value not changing
- react - the prop value remain unchanged after parents update corresponding state
- How to Make A second API call based on the value gotten from the first. with React and useEffect hooks
More Query from same tag
- How do i choose import/export in React
- Can't Display Price Data Using Ebay Finding Advanced API In React
- Passing dynamic argument to event handlers
- React hook only when props change?
- Having a null violation error when posting from my frontend to the server side in node.js and react and sequelize
- How to change the filtered date format in material table react?
- Stop Gatsby blog post slug from including individual post folders
- How to display a button only if text overflows
- How to implement animations using react router 4
- Updating state array of objects from api call
- React dynamic table rows
- Date formatting within react bootstrap table
- setting PORT and HOST manually for facebook/create-react-app production build
- Hide Chat Without losing the messages it is rendering in UseEffect
- Unable to get values form child to parent -Reactjs Redux
- How can I get navigation to stay appeared on desktop? (ReactJS)
- How to get value of react app state['Key'] in test?
- How to use custom radio component with react-final-form?
- React - Relative imports outside of src/ are not supported
- How to filter list of objects that doesn't match some condition in another list of objects
- Sorting array of objects in Redux reducer
- How to access the state of one component or the constants of one component in another component (not parent child) - React
- I am getting Cannot read property 'then' of undefined while calling the api in react
- Redux Thunk silently fails to update state
- How can I set env vars when using webpack?
- Push values in an array from an array of objects
- How can I change npm version while using create-react-app
- Styling React Modal with data attribute
- requireNativeComponent: "RNSScreenStackHeaderConfig" was not found in the UIManager when running android app
- type="number" react typescript false positive in onChange?