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

small typo #1

Open
hansschenker opened this issue Jun 6, 2022 · 0 comments
Open

small typo #1

hansschenker opened this issue Jun 6, 2022 · 0 comments

Comments

@hansschenker
Copy link

hansschenker commented Jun 6, 2022

import { debounceTime, swtichMap } from 'rxjs'; - in useObservableState

and a question: Where does "getSearchResults(search)" come from?
import { useObservableState, useAsyncValues } from 'rxooks';
import { debounceTime, swtichMap } from 'rxjs';

function MyComp() {
// an observable of searches, and a setter to set the current search
const [searches, setSearch, getSearch] = useObservableState('');

// Here we're going to compose some reactivity using RxJS,
// and subscribe to the observable to get the search results out.
const searchResults = useAsyncValues(
	() =>
		searches.pipe(
			debounceTime(500),
			switchMap((search) => **getSearchResults(search))**
		),
	[searches] // deps
);

const searchResultCount = searchResults?.length ?? 0;
const hasSearchResults = searchResultCount > 0;

const searchChange = (e) => setSearch(e.target.value);

// Maybe there's some other side effect you'd like to do with the
// current value of the observable state. You can use the getter
// for that.
const submitForm = () => {
	// Use the getter to get the most recent value
	const lastSearch = getSearch();
	doSomethingOnFormSubmit(lastSearch);
};

return (
	<form onSubmit={submitForm}>
		<div>
			<label htmlFor="search-input">Search</label>
			<input id="search-input" type="text" onChange={searchChange} />
		</div>
		{hasSearchResults && (
			<ul>
				{searchResults.map((result) => (
					<li key={result.id}>{result.text}</li>
				))}
			</ul>
		)}
	</form>
);

}

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