简体   繁体   English

如何在render()的JSX组件树中定义的组件上调用React forceUpdate()

[英]How to call React forceUpdate() on the component that is defined in JSX component tree in render()

I have the React component with schematic code: 我有带有原理图代码的React组件:

class OrderList extends Component {
    constructor() {
        super();
        this.handleUpdate = this.handleUpdate.bind(this);
    }

    handleChange(event) {
        //>>POI how to get reference to the Grid, that is defined in the component tree?
        //grid.forceUpdate();
    }


    render() {
        return (
            <div>
                <Grid data={this.props.orders}>
                <button onClick={this.handleUpdate}>Update</button>
            </div>  
            );
    }
}

<Grid> is some complex React component like https://devexpress.github.io/devextreme-reactive/react/grid/ or https://www.ag-grid.com/react-getting-started/ and its appearcance depends on some state variable. <Grid>是一些复杂的React组件,例如https://devexpress.github.io/devextreme-reactive/react/grid/https://www.ag-grid.com/react-getting-started/ ,其外观取决于在一些状态变量上。 <Grid> is re-rendered automatically (eg its final, computed (by React) style is updated) if this.props.orders is updated. 如果this.props.orders被更新,则<Grid>将自动重新渲染(例如,更新其最终的,通过React计算的样式)。 But I have this.state.selectedOrderNo variable which does not belong to the this.props.orders (of course, these are 2 different variables - array of props and scalar of state) and Grid has some styling rules that depend on this.state.selectedOrderNo but those styling rules are computed/rendered only when the <Grid> is rerendered (but this re-rendering does not happen when this.state.selectedOrderNo changes, because those styling rules are some deeper options than the direct parameters of the <Grid> ) but I would like to initiate this re-rendering of the Grid when this.state.selectedOrderNo is changed. 但是我有this.state.selectedOrderNo变量,它不属于this.props.orders (当然,这是2个不同的变量this.props.orders数组和state的标量),并且Grid具有一些依赖于this.state.selectedOrderNo样式规则。 this.state.selectedOrderNo但仅在重新渲染<Grid>时才计算/渲染这些样式规则(但是,当this.state.selectedOrderNo更改时,不会进行此重新渲染,因为这些样式规则比<Grid> ),但是当this.state.selectedOrderNo更改时,我想重新初始化Grid。 So, that is why I am trying to call grid.forceUpdate() . 因此,这就是为什么我尝试调用grid.forceUpdate() But the question is - how can I get reference grid to the <Grid component> ? 但是问题是- 如何获得<Grid component>参考grid <Grid component>

You need to use ref in Grid to make it work 您需要在Grid中使用ref使其起作用

class OrderList extends Component {
constructor() {
    super();
    this.handleUpdate = this.handleUpdate.bind(this);
}

handleChange(event) {
    //>>POI how to get reference to the Grid, that is defined in the component tree?
    //grid.forceUpdate();
    this.grid.forceUpdate();
}


render() {
    return (
        <div>
            <Grid ref={ref=>this.grid=ref} data={this.props.orders}>
            <button onClick={this.handleUpdate}>Update</button>
        </div>  
        );
}
}

Hope it helps 希望能帮助到你

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在React jsx中呈现此组件的多个副本 - how to render multiple copies of this component in react jsx 如何在React jsx中使用innerHTML渲染组件 - How to render a component using innerHTML in React jsx 如何对子组件进行 React forceUpdate()? - How to do React forceUpdate() to a child component? 如何在没有forceupdate的情况下让反应组件重新渲染? - How to get a react component to rerender without forceupdate? 如何在不将组件添加到JSX树中的情况下创建React组件 - How to create a React component without adding component to the JSX tree 如何在 React.Component.render() 中使用要渲染的部分 jsx 组件? - How to use a partial jsx component to be rendered in React.Component.render()? 反应警告:无法在未安装的组件上调用 setState(或 forceUpdate) - React Warning: Can't call setState (or forceUpdate) on an unmounted component 警告:无法在React Native中的已卸载组件上调用setState(或forceUpdate) - Warning: Can't call setState (or forceUpdate) on an unmounted component in React Native 反应-警告:无法在未安装的组件上调用setState(或forceUpdate) - React - Warning: Can't call setState (or forceUpdate) on an unmounted component 无法在已卸载的组件上调用setState(或forceUpdate)。 应对 - Can't call setState (or forceUpdate) on an unmounted component. React
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM