I'm currently developing a React app where an Application will be selected, and the waterTypes for this application should then be returned.
I therefore need to destructure the waterTypes array within my filtered ApplicationData object. The array will have either 1, 2, or 3 values present.
Currently my waterTypes variable is returning undefined to the console. How can I get this to return the waterTypes from the selected Application?
ChoicesProvider.js
import React, { createContext, useState } from "react";
export const ChoicesContext = createContext(null);
export const ChoicesProvider = ({ children }) => {
const [choices, setChoices] = useState({
category: null,
});
return (
<ChoicesContext.Provider value={{ choices, setChoices }}>
{children}
</ChoicesContext.Provider>
);
};
ApplicationData.js
const ApplicationData = [
{
name: 'App1',
waterTypes: ['Type 1']
},
{
name: 'App2',
waterTypes: ['Type 1', 'Type 2']
},
{
name: 'App3',
waterTypes: ['Type 1', 'Type 2', 'Type 3']
},
];
export default ApplicationData
Product.js
const Product = () => {
const { choices, setChoices } = useContext(ChoicesContext);
const CurrentApplication = ApplicationData.filter(x => x.name === choices.category);
const { waterTypes } = CurrentApplication;
console.log(waterTypes);
return (
<>
</>
);
};
export default Product
CurrentApplicationis going to be an array not a single object. If you want to destructure the first element in the array,var {waterTypes} = CurrentApplication[0];