I currently have a project and I need to pass the array to another component for my search bar. I am using React's Context.
I've tried passing my data but I seem to get an undefined value in my console.
Code in Context.js
Export const SearchContext = createContext();
This is the code in MainPage.js:
const data = [json_1, json_2];
const array = data.map(values => {
const search = _.flattenDeep(values);
values.search = search;
return values;
})
<SearchContext.Provider value={array} />
and in my Searchbar.js
const options = useContext(SearchContext);
console.log(options);
<AutoComplete
className="searchbar"
placeholder="Search..."
dataSource = {options}
onfilterOption={(inputValue, option) =>
option.props.children.toUpperCase().indexOf(inputValue.toUpperCase()) !== -1
}
/>
In the console.log I get an undefined value. This data should also be able to populate my search bar.
<SearchContext.Provider value={array} />if this is the exact line you have then you aren't wrapping your consumer inside the context it is going to consume