Today I saw that React (React JS) has lifecycle methods, such as
componentWillMount()
and so when the component or container is instantiated for the first time, this method will be called if it exists. (if it is defined).
One example of it is, when the component is instantiated for the first time, if the above method componentWillMount()
is defined in the class, then it will be called, and it may do an AJAX call to fetch data to populate into this component.
And the same is true for iOS or Cocoa programming:
viewDidLoad()
viewWillAppear()
and this type of "if it exists (or is defined), then call it" actually was similar to the old days of
document.getElementById("foo").onclick = function() { };
That is, if onclick
is defined, then call it. Otherwise, just ignore it. But this is related more to event handling / observer pattern / chain of responsibility, more than lifecycle methods.
(Having said, actually, you can also view lifecycle methods as event handlers too: such as, now the View or Component will be shown, I (the OS or the framework) am telling you / notifying you (about this event): if you need to do something, do it now (handle it now)).
The question is: do these lifecycle methods componentWillMount()
, viewWillApear()
a design pattern of its own, or do they belong to a bigger class or more general class of design pattern?
componentWillMount()
, the programmer can do AJAX call to fetch data particular to that component