I did a little bit of research but I found old examples with usually Redux or with one main component and cannot figure out the logic.
I can get data from an API but I do need to search, sort, filter, etc on that data (even at the same time). I know that I should not mutate the state directly, so in this case, I will have a couple of different state sets such as filteredData, sortedData, etc. However, which state am I going to pass to the Player component? How will sorting functionality will work on filtered data? I am extremely confused.
I would like to put my state and state-related logic in a Context but if this seems problematic or wrong, then I can try your approach.
Context
const ContextProvider = ({ children }) => {
const [data, setData] = useState([]);
const [filteredData, setFilteredData] = useState([]);
const [sortedData, setSortedData] = useState([]);
useEffect(() => {
fetch('URL HERE')
.then(data => data.json())
.then(data=> setData(data));
}, []);
const handleSearch = e => {
let value = e.target.value;
const filteredData = data.filter(player=>
player.name.includes(value)
);
setFilteredData(filteredData);
};
const handleSort = e => {
let value = e.target.value;
// I am trying to sort FILTERED DATA?
const sortedData = filteredData.sort((a, b) => {
if (a[value] < b[value]) {
return 1;
} else {
return -1;
}
});
setSortedData(sortedData);
};
return (
<Context.Provider
value={{ data, handleSearch, handleSort, filteredData, sortedData}}
>
{children}
</Context.Provider>
);
};
Search
const Search = () => {
const { handleSearch, handleSort } = useContext(Context);
return (
<div className='row'>
<Input
type='search'
name='search'
onChange={handleSearch}
></Input>
<Input
type='select'
name='select'
onChange={handleSort}
>
<option value='name'>Name</option>
<option value='team'>Team</option>
</Input>
</div>
);
};
Player
const Player = () => {
const {data, filteredData, sortedData} = useContext(Context);
return (
<ul>
//{data or filteredData or sortedData will be mapped?}
<li>// not sure what to do</li>
</ul>
);
};
I really appreciate code examples with some explanation.
const [finalData, setFInalData] = useState(data). And if you gotsortedDataorfilteredDatawanna display afterwards, just update thesetFinalData(toWhatDataSetYouWant)