This blog is talking about things we can do with lwc connectedCallback method. It seems to contradict itself with a certain point so I am trying to make sure.
They say that we can set property values in a connectedCallback. After that they say the following:
Do not use connectedCallback() to change the state of a component, such as loading values or setting properties. Use getters and setters instead.
This is confusing, is this some bad practice or something? I am using connectedCallback to query the server on initial load and I normally set the property in the method and it works. Just wondering if there is something I am missing here.
Best Answer
This is in fact a mistake in the LWC documentation. We are currently working on rewriting this section entirely.
The
connectedCallback
lifecycle hook is invoked when a component is connected to the document. This callback is invoked after all the public properties are set and can be used to set the state of the component.As I see it, the original author was trying to warn about the fact that public properties can be updated after the
connectedCallback
is invoked. TheconnectedCallback
is invoked only one time with the initial properties passed to the component. If a component derives its internal state from the properties, it's better to write this logic in a setter than inconnectedCallback
.To illustrate this better, let's create a component that renders a random dog picture given its breed. We can first define a utility method to fetch the dog image.
In the component below, the
getPicture
method is invoked in theconnectedCallback
. TheimageUrl
property is derived from thebreed
public property. Since the logic is in theconnectedCallback
, theimageUrl
will always have the value of the initialbreed
value. This is not ideal for our component.To solve this issue, expose
breed
using a getter/setter pair and move the logic there. Every time thebreed
public property is set, the setter is invoked and a new picture is fetched.Live example: playground