The react-router passes the parameters, and the parent component passes the parameters to the child page, which is executed twice.
react-router version
"react-router": "^ 3.0.0",
"react": "^ 15.5.4",
routing configuration
< Route path= "/ imglivedetial/:mid/:type" component= {ImageLiveDetial} / >
because parameters are entered directly in the address bar, props.parmars, is not updated, so a custom refresh props function refreshProp () is executed in both componentWillReceiveProps and componentDidMount
refreshProp () makes api requests or refreshes status. But twice every time.
solve
isn't that obvious? You can make a breakpoint in refreshProp and take a look at the stack.
the following is my guess that is not based on facts:
execute componentDidMount, once when the page is mounted to execute refreshProp
the page update phase (props change) executes componentWillReceiveProps, once to execute refreshProp.
if you want to execute it once, why not delete the refreshProp in componentWillReceiveProps.
generally speaking, my own writing is not rigorous, which can be regarded as a curve to save the nation. Change componentWillReceiveProps () to
.
componentWillReceiveProps(nextprops){
let refresh = false;
Object.keys(nextprops).forEach((value, index) => {
if(nextprops[value] != this.props[value]){
refresh = true;
}
})
if(!refresh) return;
this.refreshProps(nextprops);
}
first determine whether the nextprops has changed in componentWillReceiveProps, and then refresh the component. This temporarily solves the problem of multiple (more than two) ajax requests. If there are several prop parameters, componentWillReceiveProps will execute it several times.
then split the ajax request into separate functions to determine whether the required ajax parameter is empty, and if it is empty, it will not be executed, thus solving the problem of two ajax requests (if the request parameter is not strict, the request is also sent if it is empty. )