【发布时间】:2019-12-26 07:40:45
【问题描述】:
我有两个组件:
- 父母
- 孩子
父组件是类组件,子组件是功能组件。从父组件调用子方法的最佳做法是什么?
这些组件的目标是能够动态加载 svg 文件。
父组件:
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>
);
}
};
子组件:
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;
【问题讨论】:
标签: reactjs