Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

solid.js reactivity regarding component prop changes is not handled #22

Open
cyan-2048 opened this issue Jan 16, 2025 · 0 comments
Open

Comments

@cyan-2048
Copy link

useStore hook doesn't seem to handle component props as it should?

interface Props {
    $text: Store<string>
}


function Component(props:  Props) {
    const text = useStore(props.$text);
    return <div>{text()}</div>
}

if you replace the store passed in $text (eg. <Component $text={store} />) the store isn't actually replaced.

To solve this issue on my personal usage, I wrap the nanostore in a getter function and use createRenderEffect,
my fix may have problems I am not sure.

export function useStore<SomeStore extends Store, Value extends StoreValue<SomeStore>>(
	store: SomeStore | (() => SomeStore)
): Accessor<Value> {
	// if it's a function we do my implementation
	if (typeof store == "function") {
		const [state, setState] = createSignal(store().value);

		createRenderEffect(() => {
			const unsub = store().subscribe((val) => {
				setState(val);
			});

			onCleanup(unsub);
		});

		return state;
	}

	// Activate the store explicitly:
	// https://github.com/nanostores/solid/issues/19
	const unbindActivation = store.listen(() => {});

	const [state, setState] = createStore({
		value: store.get(),
	});

	const unsubscribe = store.subscribe((newValue) => {
		setState("value", reconcile(newValue));
	});

	onCleanup(() => unsubscribe());

	// Remove temporary listener now that there is already a proper subscriber.
	unbindActivation();

	return () => state.value;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant