I have a React functional component that does the following:
- Sends a POST to load raw data from my API
- (Among other stuff) sends mapped data to a custom table component
- Allows the user to re-send the POST with a different fiter
import React, { useEffect, useState } from "react";
import axios from "axios";
import mytable from "./mytable";
const baseRawData = [
{ name: "name", value: "John Doe" },
{ name: "sex", value: "M" }
];
export default function FuncComp() {
let [rawData, setRawData] = useState(baseRawData);
let [mappedData, setMappedData] = useState([]);
let [apiFilter, setApiFilter] = useState("all");
useEffect(() => {
setMappedData(
rawData.map(e => ({
...e
// Some logic
}))
);
}, [rawData]);
useEffect(() => {
axios.post(`/my/api/${filter}`).then(({ data }) => {
setRawData(data);
});
}, [apiFilter]);
return (
<div>
<h1>The filter is '{apiFilter}'</h1>
<mytable data={mappedData} />
<input
value={apiFilter}
onBlur={e => setApiFilter(e.target.value)}
type="text"
/>
</div>
);
}
How would I, in a class component, achieve the same logic as useEffect to map the newly received raw data every time the user changes the filter and new raw data is received?