S
SolidJS13mo ago
hannus

does createResource with multiple signals trigger when onMount

if createResource has one signal which is false as dependency, the resource will be trigger as the signal changing. However, if I set two signals as dependency, the resource triggers in every mount alougth the signals are false . Is there any methods to avoid trigger fetcher when the group signals are false? thanks a lot
4 Replies
Atila
Atila13mo ago
If I'm understanding this right,. The sourcesignals are passed as params to the fetcher even if they're false, so you can avoid fetching depending on the value of any of those. They aren't match conditions to execute createResource or not, they're params passed to the fetcher call.
const [data] = createResource(
() => [dataSignal(), moreData()],
([dataVal, moreDataVal]) => fetcher(dataVal as number, moreDataVal as string)
);
const [data] = createResource(
() => [dataSignal(), moreData()],
([dataVal, moreDataVal]) => fetcher(dataVal as number, moreDataVal as string)
);
like checking values of dataVal or moreDataVal in the example above.
Tommypop
Tommypop13mo ago
createResource will trigger whenever the value added as the source is truthy. In the case of adding multiple values, such as createResource(() => [signal1(), signal2()], () => {}), the fetcher will always run as the array will be truthy even if both values are falsy. To prevent the fetcher from running if both signals are falsy, you can use a ternary to make the source evaluate to false if both conditions are false, such as:
const [res] = createResource(
() => (cond1() && cond2() ? [cond1(), cond2()] : false),
([first, second]) => {
console.log(`First ${first}`);
console.log(`Second ${second}`);
},
);
const [res] = createResource(
() => (cond1() && cond2() ? [cond1(), cond2()] : false),
([first, second]) => {
console.log(`First ${first}`);
console.log(`Second ${second}`);
},
);
Tommypop
Tommypop13mo ago
Solid Playground
Quickly discover what the solid compiler will generate from your JSX template
hannus
hannusOP13mo ago
got it, thanks a lot thanks, I know how to use the createResrouce now.
Want results from more Discord servers?
Add your server