score:1
Accepted answer
you are close to the solution, you just need to compare the new userid
prop with the old one to prevent the infinite loop:
import { connect } from "react-redux";
import { link } from "react-router-dom";
import { fetchruns } from "../actions";
import runcell from "./runcell";
class runs extends react.component {
componentdidupdate(prevprops) {
if(prevprops.userid !== this.props.userid){ // <=== in order to prevent infinite loop add this comparison
this.props.fetchruns(this.props.userid);
}
}
componentdidmount() {
if(this.props.userid){ // <=== fetch your data inside this methode
this.props.fetchruns(this.props.userid);
}
}
renderruns() {
//show runs in reverse chronological order
const sortedruns = this.props.runs.sort(
(a, b) => new date(b.rundate) - new date(a.rundate)
);
return sortedruns.map((run) => {
return <runcell {...run} key={run.id} />;
});
}
render() {
if (this.props.runs) {
return (
<div classname="ui container">
<div classname="ui secondary pointing menu">
<h3>your training log</h3>
<div classname="right menu">
<link classname="ui button right" to="/runs/add">
add run
</link>
</div>
</div>
<div classname="ui">{this.renderruns()}</div>
</div>
);
} else {
return <div>loading runs...</div>;
}
}
}
function mapstatetoprops(state) {
return { runs: object.values(state.runs), userid: state.auth.userid };
}
function mapdispatchtoprops(dispatch) {
return {
fetchruns: (userid) => dispatch(fetchruns(userid))
}
}
export default connect(mapstatetoprops, mapdispatchtoprops)(runs);
Source: stackoverflow.com
Related Query
- Where to fetchRecords in React - componentDidMount or onComponentDidUpdate or else where?
- where to place componentDidMount in order to change link Navbar color while scrolling in React / Gatsby?
- componentDidMount equivalent on a React function/Hooks component?
- react componentDidMount not firing
- Where to declare variable in react js
- Is it ok to use ReactDOMServer.renderToString in the browser in areas where React isn't directly managing the DOM?
- Where do I fetch initial data from server in a React Redux app?
- React setState + Where does 'prevState' come from?
- Where do I initialize Firebase app in React application?
- Where do I put coding logic in my React Application
- Where do long-running processes live in a React + Redux application?
- Accessing this.props in react ComponentDidMount
- Where should I put images files in React project?
- componentDidMount method not triggered when using inherited ES6 react class
- Where to declare auxiliar functions for a React functional component. Inside or outside?
- Where to store WebRTC streams when building React app with redux
- Where to put enums in my react application
- Where to put all the screens which are common in multiple stack navigators? - React Navigation v5
- React JS: how to properly remove an item from this.state.data where data is an array of objects
- How to implement componentDidMount with hooks in React to be in line with the EsLint rule "react-hooks/exhaustive-deps": "warn"?
- Why ajax request should be done in componentDidMount in React components?
- How to TEST async calls made in componentDidMount that set the state of React Component
- React Router where to use AJAX
- React test; how to mock componentDidMount or overwrite it?
- How does react trigger componentDidMount with safari cache?
- Order of componentDidMount in React components hierarchy
- React Redux - How to dispatch an action on componentDidMount when using mapDispatchToProps in a connected component
- onKeyUp not working in React where onChange did
- React - useEffect hook - componentDidMount to useEffect
- React componentDidMount vs getInitialState
More Query from same tag
- migrate js to tsx in react
- Syntax error: Unexpected token, expected { (1:7) , create-react-app
- React props return as undefined
- Lifecycle issue on axios call to an external API
- mapDispatchToProps vs. store.dispatch()
- How to increase the height of the react-native Slider component?
- onClick open module
- React Material onClick Collapse [Answer Updated]
- Putting React templates in properties of one global variable
- How can I avoid the infinite loop in useEffect?
- PDF to Editable Documents
- How can I conditionally disable a control in Storybook based on the value of another argument?
- Can't display an image in react from backend django
- Utilizing componentDidMount when switching between different instances of the same React component
- how to test if babel works and my plugins are executed
- ReactJS: doing calculations to setState, and updating
- React Ionic hooks Component setValue isnt working
- Optional generic type and corresponding parameters for TypeScript
- How to pass methods to form provider with different name to handle nested forms
- React keys - rendering 2 duplicate items in a list- how to avoid same key warning as same items are intended without using index as key?
- How to avoid "TypeError: popper.js.map is not a valid URL" message
- React JS this.props.data not defined in getInitialState for child component even though it is defined in render (and is defined in parent)
- Material-UI: A component is changing the uncontrolled checked state of SwitchBase to be controlled
- Dynamic TreeView of a directory in React
- Timestamp field from firestore showing an invalid date if the field does not exist
- What is a react way to do this?
- How do I get the github user info to show up using github api in react?
- How to filter and sort the same array of object state in redux?
- why setItem aren't letting 'edit button' work?
- How can I effectively store data in array?