score:0

Accepted answer

accessing barchart.data seems wrong as, barchart is basically a functional react component.

what you want is probably this:

  1. you have a data array.
  2. you want to show that data in a barchart
  3. you want to give a button to the user such that when user clicks that button, a new row is added into that data array.
  4. and when that data array changes, you want the barchart to refresh and display the updated data array.

if this is the case, then i would recommend checking the lifting state up in the react docs. it is pretty much the same use case here. you have a single data source shared across 2 components.

also, check the usage of react hooks for checking state usage: https://reactjs.org/docs/hooks-state.html

here is a pseudocode you can try:

parentcomponent = () => {

    // 1. create your data state here using react hooks
    const [data, setdata] = usestate([someinitialdata]);
    
    return (
        <div>
            // 2. barchart now relies on the data in the state
            //    whenever the data updates, the chart refreshes
            <barchart data={data}/>
            // 3. pass setdata to the component and trigger it with the 
            //    button's onclick handler
            <buttonchart updatedata={setdata}/>
        </div>
    );
}

Related Query

More Query from same tag