2

私は反応するのが比較的新しく、それを理解できないようです。これをしばらく調査しましたが、何も機能していないようです.

createRef を使用している親コンポーネントがあります

class Parent extends React.Component {
    constructor(props) {
        super(props);
        this.chartRef = React.createRef();
    }

次に、それを子に渡します & アクセスは次のようになります

<Grid item xs={12}>
   <Child
      ref={this.chartRef}
    />
    <Button onClick={this.getState}> get ref info</Button>
</Grid>

しかし、getState chartRef では current は常に null です

getState = () => {
        console.log(this.chartRef.current);
    };

これが子コンポーネントです

class Child extends React.Component {
    componentDidMount() {
        this.props.registerPlugins.forEach(plugin => {
            Chart.pluginService.register(plugin);
        });
    }

    render = () => {
        const { data, options, plugins, height } = this.props;

        const updatedOptions = {
            ...options
        };

        return <div>
            <Line
                height={height}
                data={data}
                options={updatedOptions}
                plugins={plugins}/>
        </div>;
    };
}


Child.propTypes = {
    height: PropTypes.number.isRequired,
    data: PropTypes.object.isRequired,
    options: PropTypes.object.isRequired,
    plugins: PropTypes.array,
    registerPlugins: PropTypes.array,
};

export default Child;

どんな助けでも大歓迎です

4

2 に答える 2