简体   繁体   English

ReactJS:组件属性更改后重新渲染不起作用

[英]ReactJS: re-render not working after component's property is changed

I am having a react issue and I am not seeing where the issue could be. 我有一个反应问题,但看不到问题可能在哪里。 For the code below: I created an accordion component that will display and hide depending upon the value of a state variable called displayForm. 对于下面的代码:我创建了一个手风琴组件,该组件将根据状态变量displayForm的值显示和隐藏。

When someone presses a button internal to the InlineListComponent, it should make the form inside InlineListComponent appear. 当有人按下InlineListComponent内部的按钮时,应该使InlineListComponent内部的表单出现。 I am passing the this.displayTireAccessories in the displayAction property. 我在displayAction属性中传递了this.displayTireAccessories。 I am resetting the state variable for displayForm from false to true, which I thought would then trigger the InlineListComponent to re-render since the displayForm property value has changed. 我将displayForm的状态变量从false重置为true,由于displayForm属性值已更改,因此我认为这将触发InlineListComponent重新呈现。 This does not seem to be the case. 似乎并非如此。 I added console.log statements to displayAction and the render method for InlineListComponent. 我将console.log语句添加到displayAction和InlineListComponent的render方法。 I can see where the value is getting updated in the displayForm, however, I am not getting any console.log to print from the InlineListComponent. 我可以在displayForm上看到值的更新位置,但是,没有从InlineListComponent打印任何console.log。

What am I missing in the rules for re-rendering that would make this not work? 我在重新渲染的规则中遗漏了什么,使它无法正常工作?

<InlineListComponent
    orientation="Vertical"
    options={[{name: 'Tires', value: '1'},
        {name: 'Rims', value: '2'},
        {name: 'Hubcaps', value: '3'}]}
    text="name"
    label="Tire accessories (you must enter at least one)"
    key="tireAccessories"
    action={async (item, e) => {
        await this.displayTireAccessories(item);
    }}
    displayAction={async (e) => {
        await this.displayTireAccessories({});
    }}
    displayForm={this.state.displayForm}>
      <TireAccessories addAccessory={this.addAccessory}/>
</InlineListComponent>

this.displayTireAccessories = async (item) => {
  const {dispatch} = this.props;
  await this.setState({displayForm: true});
}

constructor(props){
    super(props);
    this.state = {
        displayForm: true
    }
}

You can't directly await a setState , it doesn't return anything. 您不能直接await setState ,它不会返回任何内容。 BUT you can use its callback argument to make it a promise that you can await : 但是您可以使用其回调参数来保证您可以await

new Promise((resolve) => {
  this.setState(state, resolve)
});

By the way, everytime you have an async function that just awaits one promise at the end of the function, you can discard the async await keywords and just return the promise. 顺便说一句,每当您有一个异步函数仅在函数末尾等待一个promise时,就可以丢弃async await关键字并仅返回promise。

So : 因此:

async (e) => {
    await this.displayTireAccessories({});
}

becomes : 变成:

(e) => this.displayTireAccessories({})

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM