score:1
so the useeffect
implementation here takes place when the component is rendered for the first time. thats when you get the result:
object {contacts: array[0]} object {contacts: array[1]}
since you are still in the app, the action dispatched at the first render of the component is saved in the state. since you have navigated away component .i.e. the component is not longer in dom, when you load component again in dom, hence after the initial render, the useeffect
takes place and renders your component after the effect is completed because you are dispatching another action to the reducer even though you have the value.
since a new action is dispatched, that is considered as a state change and that is when you see following:
object {contacts: array[1]} object {contacts: array[1]}
you can test this by add console.log
at this line in your reducer:
now you can add something like a check or a flag that can confirm that the data has been loaded and you don't have to make the fetch again.
so i have added following check in your useeffect
:
if (state.contacts.length === 0) {
fetchdata();
}
and eureka !!! one less render.
i have tried out and modified your sandbox on this link. so it is nice to have these check incase you want to minimise unnecessary call and renders on the app.
score:1
it is because your component is mounting again when you change your route. so when you go from contact list
to add contact
:
your reducer already set it to name:bob
and now again when your come back to your component list
component. two things is happening component is rendering because of usecontext
inital state(hooks also cause rerendering) and another one is causing because of your useeffect
i.e you are dispatching your fetch_contacts
when component is mounted.
if you see console on first load:
object {contacts: array[0]}
object {contacts: array[1]}
observe console return two statement first state is when contact is empty and second state is when contact is updated using reducer]
now when you come from add contact page the console shows this:
object {contacts: array[1]}
object {contacts: array[1]}
both has same value first one coming from context intital state and second one is coming again as reducer is again updating value.
if you want to see yourself add console more for clarity. check here how component is mounting and unmounting: https://codesandbox.io/s/elegant-ishizaka-enck6?file=/src/components/contactlistpage.js:478-492
Source: stackoverflow.com
Related Query
- Why does my component render twice when using React's Context API and the useEffect hook?
- Why does the render function in react is called twice when using the component strategy?
- Why does useState cause the component to render twice on each update?
- Why does data from my api only display once and when I refresh the page it gives an error
- Why doesn't the loader show on the second test when I run the same test twice in jest, using react testing library, react.lazy, and suspense?
- Why does react re-render twice when using the state hook (useState)?
- Why does my React app menu open when I am only changing the state and am not using any CSS?
- Why are the arrays null when I'm sending form data using .NET API and React?
- Why does the return value change when using React-icon component for my button?
- Reducer running twice when using useReducer and React Context API
- When we render react Components from an iterable using Array.prototype.map() how is the index generated, and how does react uses it?
- Why does my React component not render when using React-Router?
- How do I use react context API to pass data from parent component to child component when using react-router in the parent component in React.js?
- Access child function from parent using refs when the child is a function component and the parent does not return JSX
- Why does not the background color get applied from props using state for a styled component div using react and typescript?
- When using create-react-app why does the development server keep disconnecting?
- How to pass the match when using render in Route component from react router (v4)
- ReactJS how to render a component only when scroll down and reach it on the page?
- Using an API call in ComponentDidMount to update Redux store makes my component render twice
- Why is my React checkbox onChange handler firing on render and then not when the box is clicked?
- New React context API and flow error, when trying to render Consumer
- Why memory leak happend and render slowing down when I close and re-open react component (material-table)?
- Getting the ref from a dynamic component when using Redux, React and react-router-dom 4.x
- v16.6.0 or higher Context API not working when child is used in component that provides the context
- Why does this component render twice in react?
- MaterialUI: Why does setting background color on Select component hides the InputLabel text and how could I avoid it happening?
- Why do I get a 404 for half a second when clicking a next.js `Link` that just calls an API endpoint and then navigates to the correct page?
- Typescript and eslint issue when using styled-components and Material UI: `React does not recognize the `showText` prop on a DOM element`
- Why does React render component for the second time after setting the state to the same value?
- Why is my state replacing my posts when I add a new one using React hooks and React context
More Query from same tag
- how to improve my coding standards to meet senior developer standards
- React/Redux: how to hold login fail
- How to update state of component in componentDidUpdate() without being stuck in an infinite re render?
- Nest components from an array
- How to do a custom interactive "map" with an image with React?
- material-ui <Grid item> component's children with 100% width overlaps parent
- How to pass custom attribute to a styled component in a functional component?
- How can I pass a URL querystring variable dynamically to a composed graphql query in react?
- Unable to parse props in child component
- How to not clear input field onClick
- error when trying to fetch data from server Parsing error: Unexpected token
- Antd 4.0.0: How to use form.getFieldValue() and all form's APIs by `const [form] = Form.useForm`?
- Axios: Basic auth not working with GET request
- setInterval in UseEffect getting call 2/4 times
- React/Flux way to handle permission sensitive actions with login flows
- React/NextJS using UseEffect Error: Rendered more hooks than during the previous render
- Using react hooks inside render prop function
- My react component is not displaying despite having no errors
- React on different subdomains
- Updating property inside a nested array of object
- React doesn't take well the td colSpan attrib
- How can I measure wasted renders in React 16?
- React Redux pass props to other component enhancer
- React Synthetic Events Bubble Capture with Components
- React Load hashed image from data while loading Image
- How to pass event in onClick in ReactJS
- I am not able to use useHistory function in action creator file
- Override quill toolbar's after selector
- 'Error: Objects are not valid as a React child (found: object with keys {results, info})'
- Mobx cannot resolve observable property decorator