Problem: Searching is working only on one page not others
I have hit a roadblock in my project that has an issue is not being able to search from the rest of the pages that have the Travis component(Travis = Header), The Travis Component only works in SearchResult Page hence I have difficulty in solving this problem. Help will be grateful!
Header.js
export default function Travis({ onPlaceChanged, onLoad }) {
return (
<Autocomplete className="search" onLoad={onLoad} onPlaceChanged={onPlaceChanged}>
<div className="form">
<Search className="ml-2 text-slate-600"/>
<InputBase
type="text"
id="location"
className="input"
ref={primaryLocationRef}
placeholder="Where are you going to Explore?"
required
/>
</div>
</Autocomplete>
)
}
SearchR.js
import React, { useState, useEffect } from 'react';
import { getPlacesData, getWeatherData } from '../api/travelApi';
import Travis from '../components/Header';
import Map from '../components/Map/Map';
import SearchResults from '../components/Map/SearchResults';
import Footer from '../components/Footer';
const SearchR = () => {
const [type, setType] = useState('hotels');
const [rating, setRating] = useState('');
const [coords, setCoords] = useState({});
const [bounds, setBounds] = useState(null);
const [weatherData, setWeatherData] = useState([]);
const [filteredPlaces, setFilteredPlaces] = useState([]);
const [places, setPlaces] = useState([]);
const [autocomplete, setAutocomplete] = useState(null);
const [childClicked, setChildClicked] = useState(null);
const [isLoading, setIsLoading] = useState(false);
useEffect(() => {
navigator.geolocation.getCurrentPosition(({ coords: { latitude, longitude } }) => {
setCoords({ lat: latitude, lng: longitude });
});
}, []);
useEffect(() => {
const filtered = places.filter((place) => Number(place.rating) > rating);
setFilteredPlaces(filtered);
}, [rating]);
useEffect(() => {
if (bounds) {
setIsLoading(true);
getWeatherData(coords.lat, coords.lng)
.then((data) => setWeatherData(data));
getPlacesData(type, bounds.sw, bounds.ne)
.then((data) => {
setPlaces(data.filter((place) => place.name && place.num_reviews > 0));
setFilteredPlaces([]);
setRating('');
setIsLoading(false);
});
}
}, [bounds, type]);
const onLoad = (autoC) => setAutocomplete(autoC);
const onPlaceChanged = () => {
const lat = autocomplete.getPlace().geometry.location.lat();
const lng = autocomplete.getPlace().geometry.location.lng();
setCoords({ lat, lng });
};
return (
<>
<Travis onPlaceChanged={onPlaceChanged} onLoad={onLoad}/>
<main>
<Map
setChildClicked={setChildClicked}
setBounds={setBounds}
setCoords={setCoords}
coords={coords}
places={filteredPlaces.length ? filteredPlaces : places}
weatherData={weatherData}
/>
<SearchResults
isLoading={isLoading}
childClicked={childClicked}
places={filteredPlaces.length ? filteredPlaces : places}
type={type}
setType={setType}
rating={rating}
setRating={setRating}
/>
</main>
<Footer />
</>
);
}
export default SearchR;
Here is one of the page that has Travis Component
Index.js
import React, { useState } from 'react';
import Footer from "../components/Footer";
import Hero from '../components/Hero';
import Banner from '../components/Banner';
import Hosting from '../components/Hosting';
import Article from '../components/Articles/Article';
import Travis from '../components/Header';
export default function Index() {
return (
<>
<Travis />
<main>
<Hero />
<Banner />
<Article />
<Hosting />
</main>
<Footer />
</>
);
}