S
SolidJS15mo ago
Ryan

CreateEffect has infinite loop

So I am having an infinite loop issue with an effect that I am trying to figure out how to handle without having to resort to createReaction() since I find those are less than ideal to work with but maybe that is the only SingleInFormAutoShowOptions. I have this code:
createEffect(() => {
setValue('fieldName', 'field value');
});
createEffect(() => {
setValue('fieldName', 'field value');
});
Now the problem with this is the setValue() internally sets whether or not the fieldName has been "touched" and to do that, I need to get the existing touched fields to add the new one to and since that gets the touched fields, that causes the effect to run over and over again. Is there a better way to handle this?
2 Replies
REEEEE
REEEEE15mo ago
If you need access to the current touched fields, you can access the current values in the setter
createEffect(() => {
setValue('fieldName', c => ...);
});
createEffect(() => {
setValue('fieldName', c => ...);
});
optionally, use untrack
createEffect(() => {
const currentValue = untrack(value)
setValue('fieldName', currentValue + 'new value');
});
createEffect(() => {
const currentValue = untrack(value)
setValue('fieldName', currentValue + 'new value');
});
https://www.solidjs.com/docs/latest/api#untrack
Ryan
Ryan15mo ago
totally forgot about that (which is funny since I am using that method everywhere else), Thanks.