As the question state, what I am trying to do is pass a child value from child to parent. I want to call the child variable in my parent function.
Parent code
function App() {
let [value, setVal] = useState(null);
...
const onLayoutChange = (layout) => {
console.log(value);
if (value != null) {
value.resize();
}
// chartInstance.resize();
};
return (
<ResponsiveGridLayout
layouts={layout}
onLayoutChange={() => onLayoutChange(layout)}
>
<div key="1">
<Card style={styles} val={setVal} />
</div>
</ResponsiveGridLayout>
);
}
#1 child code
export default function SimpleCard(props) {
return (
<Card style={props.style.main}>
<CardContent>
<NewvsReturnVisitors value={props.val} />
</CardContent>
</Card>
);
}
#2 child code
const Newvsresturnvisitors = (props) => {
...
let [val, setVal] = useState(null);
let chartInstance = null;
props.value(val);
function renderChart() {
const renderInstance = echarts.getInstanceByDom(chart.current);
if (renderInstance) {
chartInstance = renderInstance;
setVal(chartInstance);
} else {
chartInstance = echarts.init(chart.current, null, {
width: 500,
height: 300
});
setVal(chartInstance);
// chartInstance.resize();
}
chartInstance.setOption(option);
}
...
}
export default Newvsresturnvisitors;
I am not sure if this is possible, any advice is really appreciated.
This is the link to my codesandbox