score:1
Accepted answer
i added init
to your store:
// @datareducer.js
export const initialdatastate = {
init: true,
posts: []
};
const datareducer = (state, action) => {
switch (action.type) {
case 'all':
// init false
return { ...state, posts: action.payload };
case 'inc':
return { ...state, init: false, posts: [...state.posts, action.payload] };
...
}
// @app.js
function app() {
const [{ init, posts }, dispatch] = useglobalstate();
useeffect(() => {
init ? getinc(dispatch) : getall(dispatch);
}, [init, dispatch]);
...
}
Source: stackoverflow.com
Related Query
- Using the same data dependently in the same useEffect
- React Hook useEffect : fetch data using axios with async await .api calling continuous the same api
- I can't fetch data from the api using useEffect
- how to delete all data and the update the page without refreshing using useEffect
- How do I correctly add data from multiple endpoints called inside useEffect to the state object using React Hooks and Context API?
- Can I use mock data and proxy api data at the same time in my react project when using node express as a proxy server?
- Updating the added data using the same form | Reactjs
- Filter react-table and react-leaflet marker at the same time using textfield (Show filter data in table and marker in the map)
- Heatmap multiple data labels for the same axis in dynamic way using javascript
- How to fetch and get data from one .json data to another .json data using the same linked id?
- Unable to display the data by using useEffect hook inside MaterialTable
- react useEffect how to update data if two different dependency arrays updates at the same time
- React: Send post request to server using the data with the state updated in useEffect
- How do I upload a file using Fetch and Multer and post form data in the same request?
- Downsides of using two UI libraries in the same react project?
- Using Lodash debounce in React to prevent requesting data as long as the user is typing
- Is it possible to call a reducer function from another reducer function (within the same slice) using Redux Toolkit?
- When is the cleanup function triggered when using useEffect hook with dependencies?
- How to persist to both localStorage and sessionStorage at the same time using redux-persist?
- How do you pass data when using the navigate function in react router v6
- How to chain async/await using for data that depends on the first call
- using useEffect in combination with Axios fetching API data returns null - how to deal with this?
- How can I cache data that I already requested and access it from the store using React and Redux Toolkit
- Is it wrong to fetch an API without using the useEffect Hook?
- How to generate a unique id automatically for the data that is posted to an API server using axios library in redux
- Using Flux to build an edit form, who actually POSTs data to the server: actions, stores, views?
- How can I use react useContext , to show data from context, in the same component
- What's the recommended place to fetch user data using Redux when app starts?
- Avoid old data when using useEffect to fetch data
- VS Code - open a file using the same window and keeping the workspace
More Query from same tag
- React Lazy Loading Issue in 16.13.0
- How do i convert code to retrieve data from Firebase V8 to Firebase V9 in Next.js
- Populate Dropdown Select with Props React
- How to make svg fit container in React?
- Get the latest key param of multiple params from a single URL?
- How to render a Backbone view inside a React component?
- I cant get my state to update using hooks
- React: How to render a parent component from child component?
- Unable to make api calls from React Single Page app
- How to populate a div from the data from fetch
- TypeError: setEmail is not a function
- React router v6 outlet doesn't seem to work
- React state variable not accurate after set state in useState hook
- How to set states with props in react.js ES6
- Uncaught TypeError: (0 , _react.createClass) is not a function
- How to auto open react DatePicker
- Two pages overlapping each other
- Custom CSS doesn't override NavLink in React Bootstrap
- Best practice interface for custom hooks with function arguments
- Set default value to filter
- Deploy react-app-rewired and react-app-rewire-less on Heroku
- Material-UI TreeView TreeItems background
- how to set inline style in react jsx
- react hooks useEffect() cleanup for only componentWillUnmount?
- Word Counter with React
- Dynamic meta tag not being displayed on fb sharing debugger
- How to combine 'onKeypress' and 'onClick' event in a single function
- Reactjs drag and drop
- React infinite recursion on history.push and useEffect
- Cant fetch data from child component react