A quick hack solution
A quick hack solution that I’ve used in the past can make this easier, but it has drawbacks.
We know that when the display function runs initially, everything is displayed correctly according to the settings at that time. Therefore, an easy way around this is to simply run that display function again after updating the settings.
toggle.onChange(async (value) => {
plugin.settings.firstToggle = value;
await plugin.saveSettings();
});
In order for this to work, make sure you empty the DOM container at the start of the display function. It may not be necessary on it’s first run, but it will be on subsequent runs.
containerEl.empty();
Lastly, there are big drawbacks to this method. When the tab is rebuilt using the display method, the DOM container shrinks to zero height when it’s cleared and is then populated again.
The user won’t notice this happen, however if the tab is longer than the screen it will be scrolled to the top — Which is a horrendous user experience and quite disorienting.
It also completely breaks keyboard accessibility because it moves the keyboard focus back to the top as well.
So, in short, you can use this.display() to refresh the settings tab, but only so long as your settings tab is very short and you’re still in development.
It’s a good way to start quickly while you’re figuring out the settings layout, but you’ll need to adjust it before publishing to maintain the right level of user experience.