My SolidJS component fetches a JSON object from a server. I can use the SolidJS For
loop to process the result, but I cannot use the vanilla javascript filter function.
function YearSearch(props) {
const [years] = createResource(() => server.fetchData("json/list-of-years"))
return (
<Show when={ years() }>
<For each={ years() }>
{(num, key) =>
<p>This works... { num.name }</p>
}
</For>
</Show>
)
}
The code above works as expected, but the code below returns the error "years.filter is not a function".
function YearSearch(props) {
const [years] = createResource(() => server.fetchData("json/list-of-years"))
return (
<Show when={ years() }>
<For each={ years.filter(num => num.name.includes(searchString)) }>
{(num, key) =>
<p>This returns an error... { num.name }</p>
}
</For>
</Show>
)
}
Note: searchString
is a SolidJS Signal set by an input field.
I believe this fails because years
or years()
is a custom SolidJS thing rather than a simple array.
So my question is: How do I convert years()
into an array that can be filtered?