I’m trying to display (and update live) my application logs so I can watch them via the web. The issue is, when I call updateLogs() it doesn’t re-render the each, it never does, even when calling updateLogs() in mount for the first time.
Each loop:
{#each application.logs as log }
<p>{log}</p>
{/each}
updateLogs:
function updateLogs() {
axios.get('http://localhost:5000/applications/' + application.Id + '/logs')
.then(function (response) {
application.logs = response.data;
})
.catch(function (error) {
console.log(error);
});
}
Declaration:
let application = {
logs: []
};
3
Answers
I’m thinking of 3 ways you can achieve this.
$:
instead oflet
to declareapplication
key
block to watch and re-render the child block(#each)
every time the watched variable changes; in this example(application.logs)
.application.logs
again just to confirm, works sometimes… I don’t know how and why..Update your updateLogs() function to this:
Svelte doesn’t consider
application.logs = response.data
as a reactive assignment. You can either update the object and then reassign it to itself or use the ES6 spread syntax like I did. You can learn more about it here.Thanks!
Since the assignment was mentioned three times
is one and there’s no problem with that here but probably with the response / returned data
REPL