I'm creating a React Movie app which enables user to search for movies and add to his favorite list. This app is going to have a live search functionality with infinite loading feature so when user enters a title name a Modal will display results and user can select titles to be added to his list.
I'm so confused about managing live search and infinite loading state and their dependency. So is this approach correct or search results should be part of live search component?
const App = () => {
const [searchResults, setSearchResults] = useState([]);
const [reqQuery, setReqQuery] = useState("");
useEffect(() => {
// Request from API and store in the searchResults
}, [reqQuery]);
useEffect(() => {
// Render Results
}, [searchResults]);
return (
<>
<SearchForm onConfirmedInput={(query) => setReqQuery(query)} />
{searchResults && <Results data={searchResults} />}
</>
);
};
const SearchForm = (props) => {
const [value, setValue] = useState("");
const handleChange = (e) => {
const val = e.target.value;
setValue(val);
// If value is valid then lift-up
props.onConfirmedInput(value);
};
return <input value={value} onChange={handleChange}></input>;
};
P.S: Mentioned code is just to elaborate the issue and is not part actual app code