I have a React component that fetches data using the useEffect
hook like so:
const cache = {key: "data-fetched-using-key"}
function Config({key, options}) {
const [data, setData] = useState();
useEffect(() => {
const fetchedData; // fetch data using key and options
setData(fetchedData);
cache[key] = fetchedData;
}, [key, options])
return <p>{data}</p>;
}
This runs the hook every time key
or options
change. However, I'm also caching the data locally, and only want the effect to run when both key
AND options
change (since for each key/options combination the data will always be the same).
Is there a clean way to depend on the combination of key
AND options
rather than key
OR options
using React Hooks?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…