I have two components:
- parent
- child
The parent is class component and child is functional component. What is best practice to call child methods from the parent component?
The goal of these components is to be able to load svg file dynamically.
Parent component:
class UnitMonitor extends PureComponent {
constructor() {
super();
}
state = {
img: null,
};
onChangeShcema = schemaID => {
axios.get("/api/schemata/get-schemata-nodes/" + schemaID).then(response => {
let path = response.data["0"]["file"];
// call loadFile function of child component is needed
});
render() {
return (
<Row type="flex" className="">
<Col span={25}>
<SvgViewer />
</Col>
</Row>
);
}
};
Child Component:
const SvgViewer = () => {
const loadFile = path => {
let svgFile = require("./images/" + path);
const svg = document.querySelector("#svgobject");
svg.setAttribute("data", svgFile);
};
return (
<div className="unit-schema-container1">
<object id="svgobject" type="image/svg+xml" data={null}></object>
</div>
);
};
export default SvgViewer;