FelipeEmos
FelipeEmos
Explore posts from servers
SSolidJS
Created by Shonty on 1/28/2024 in #support
How to trigger createEffect when navigating to the same URL ?
Your welcome, glad I could add something haha 🙌
9 replies
SSolidJS
Created by Shonty on 1/28/2024 in #support
How to trigger createEffect when navigating to the same URL ?
Nice, this seems pretty decent The "run too much" aspect of it is probaby negligible and you shouldn't be too worried about edge cases in my opinion If this starts giving you some problems or you want to be extra careful about such "edge cases", you can always do a diffing yourself! That should be easy, you have to track the last time you've run the effect and do an early exit if necessary. Maybe save the "lastUrl" in a variable and if the current "url" is equal to the "lastUrl" do nothing, just early exit. Then if it changed you update the "lastUrl" and do your computation 🚀
9 replies
SSolidJS
Created by Shonty on 1/28/2024 in #support
How to trigger createEffect when navigating to the same URL ?
Buuuut maybe you could just change the dependency to look for the whole location, does that work? I'm curious Something like changing your code from
createEffect(on(() => location.search, () => {
const search_params = new URLSearchParams(location.search).get(props.button_name);
set_button_value(search_params ? search_params : '');
}))
createEffect(on(() => location.search, () => {
const search_params = new URLSearchParams(location.search).get(props.button_name);
set_button_value(search_params ? search_params : '');
}))
to
createEffect(on(() => location, () => {
const search_params = new URLSearchParams(location.search).get(props.button_name);
set_button_value(search_params ? search_params : '');
}))
createEffect(on(() => location, () => {
const search_params = new URLSearchParams(location.search).get(props.button_name);
set_button_value(search_params ? search_params : '');
}))
9 replies
SSolidJS
Created by Shonty on 1/28/2024 in #support
How to trigger createEffect when navigating to the same URL ?
I don't think it's worth it to go for the "navigate just to retrigger" thing as you suggested last.... I agree it's not clean, a little hacky If you wanted to solve things in a similar "not ideal" solution you could put a manual reset trigger on your specific action that is troublesome. Here's a code ChatGPT created when I prompted our conversation
const Button: Component<{button_name: string}> = (props) => {
const location = useLocation();
const [button_value, set_button_value] = createSignal('');
const [resetTrigger, setResetTrigger] = createSignal(0);

const updateButtonValue = () => {
const search_params = new URLSearchParams(location.search).get(props.button_name);
set_button_value(search_params ? search_params : '');
};

createEffect(on([() => location.search, resetTrigger], updateButtonValue));

// Function to handle reset
const resetFilters = () => {
// Navigate to the URL without query params
// ...

// Trigger the effect
setResetTrigger(count => count + 1);
};

// JSX and other logic
// ...

return (
<>
{/* Some code here */}
</>
);
};
const Button: Component<{button_name: string}> = (props) => {
const location = useLocation();
const [button_value, set_button_value] = createSignal('');
const [resetTrigger, setResetTrigger] = createSignal(0);

const updateButtonValue = () => {
const search_params = new URLSearchParams(location.search).get(props.button_name);
set_button_value(search_params ? search_params : '');
};

createEffect(on([() => location.search, resetTrigger], updateButtonValue));

// Function to handle reset
const resetFilters = () => {
// Navigate to the URL without query params
// ...

// Trigger the effect
setResetTrigger(count => count + 1);
};

// JSX and other logic
// ...

return (
<>
{/* Some code here */}
</>
);
};
It makes sense
9 replies
SSolidJS
Created by Shonty on 1/28/2024 in #support
How to trigger createEffect when navigating to the same URL ?
Your question has an interesting phrasing... what you do you want to do with that possibility? Maybe you should look at the problem at a different angle... so what's your problem? Have you considered something like just putting an even listener to whenever the router changes routes?
9 replies