In a react component you can have both functions as shown below.
getInitialProps() {
return fetchProps();
};
getDefaultProps() {
return this.defaultProps;
}
Can getDefaultProps ever be called in the react lifecycle?
I'd imagine you'd want to do some try catch in getInitialProps that grabs the defaultProps whenever it fails to fetchProps :
getInitialProps() {
try{
return fetchProps();
} catch(error){
return getDefaultProps();
}
};
Seems strange to me.
I know getInitialProps() is used for async behavior while getDefaultProps() is more for setting up the props for a component so it can still render before the props are passed.
So to answer your question I dont think so. you cannot call getDefaultProps() once it is rendered.
This would be an example of using getInitialProps()
Page.getInitialProps = () => {
return fetch('https://api.github.com/repos/developit/preact')
.then(res => res.json())
}
While getDefaultProps() would work more like this
getDefaultProps: function() {
return {
name: 'Mary'
};
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.