Here is my code.I'm using an API call in useEffect just like following:
const [data, setData] = useState([]);
const getQuotationById = async () => {
const resp = await axios.get(`${process.env.REACT_APP_API_URL}quotation-details/${id}`);
setData(resp.data);
};
useEffect(() => {
getData().catch((e) => {
console.log('error');
console.log(e);
});
}, []);
return <div>
{data.quantities.split('/').map((quantity, index) => (<span>quantity</span>)
</div>
The interesting thing is that an error TypeError: Cannot read properties of undefined (reading 'split') in react always comes out.
But there is no error if I use the optional chain in the return sytanx like:
return <div>
{data.quantities?.split('/').map((quantity, index) => (<span>quantity</span>)
</div>
Why this happens?
dataof this component, maybe i am wrong, why useprops.data?propscome from and what does it have to do with your component's state?