0

I am trying to render an element from a Float32Array using Svelte.

let laterTimeIndex = hourlyWeatherData.time
    .find((timeData: Date) =>
        timeData.getHours() === (currentTime + 5) % 24
    );
let laterTemperature = hourlyWeatherData.temperature_2m[
    hourlyWeatherData.time.indexOf(laterTimeIndex)
];
$inspect(laterTimeIndex);
$inspect(laterTemperature);
<div class="weather">
    <div class="weather-card weather-now">
        <p>Weather Now: {currentPeriod}, {currentWeatherData.temperature_2m}</p>
    </div>

    <div class="weather-card weather-later">
        <p>Weather Later: {laterPeriod}, {laterTemperature}</p>
    </div>
</div>

For context, hourlyWeatherData is a prop that contains multiple Float32Array. The arrays are return results of an API. Array temperature_2m was one of the Float32Array.

I was able to get the value when I used $inspect rune but cannot get the value to render on the browser like shown belowexpect value to render but nothing

What could be the issue and are there workarounds I can try to get around the issue?

3 Answers 3

2

If the prop can change (i.e. data from the API is assigned later), you probably have to wrap your values in $derived.

The fact that the data is a Float32Array does not matter for display purposes, you can test this by just adding {new Float32Array(1)[0]} in the template, it shows the default value 0.

If you are modifying the array (e.g. assigning specific indices), you will not get updates because the data structure itself is not reactive. You would have to assign a new instance instead.

Sign up to request clarification or add additional context in comments.

Comments

-1

The value isn’t rendered because the Float32Array isn’t reactive in Svelte when mutated. You must assign a new instance (or use $derived) so Svelte detects the change and updates the display.

Comments

-1

While trying and debugging the suggested answer, I noticed that the value I was inspecting was rendered for a split second before disappearing. This means I was able to get the value from Float32Array but was not followed to the browser.

I called the API within a PageServerLoad load function at the time. Once I moved the API call to Load load function, I was able to render the value I was trying to get. I think the API return only run "server-side" and not being followed through the client (hydration)

2 Comments

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
The data should transfer correctly from a server load function. If it disappears on client-side render, there might be some other issue that causes a hydration error, e.g. incorrectly nested elements that lead to a different DOM structure when the browser parses it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.