简体   繁体   English

ReactJS 使用 if-rendering 时有组件消失

[英]ReactJS when using if-rendering there is component that disappears

Here is the question:这是问题:

I have a ReactJS app with an if-statement rendering.我有一个带有 if 语句渲染的 ReactJS 应用程序。 After getting response from post-query to API I need to render component, but it appears for a second and then disappears.从后查询到 API 获得响应后,我需要呈现组件,但它出现一秒钟然后消失。 Where is the problem?问题出在哪儿? Thank you!谢谢!

handleSend() {
    try {
      axios.post('/login', {  posted_data: this.state.value, 
                              window_size: this.state.range_val, 
                              sin: this.state.value1, 
                              sin_v: this.state.value2 }).
                              then(response => this.setState({ token: response.data['data'] }));
         
    } catch (e) {
      console.log(`😱 Axios request failed: ${e}`);
    }
  }  
  
  render() {
    return (
      <div className = "MarkdownEditor">
        
        <Form> 
          ...
          <Button variant="primary" type="submit" onClick={this.handleSend}>
            Найти
          </Button>
          { this.state.token == "a" && 
            <h2> {this.state.token} </h2>
          } 
        </Form>
  
      </div>
    );
  }
}

If the Form component represents an actual HTML form element then you need to prevent the default behaviour when your button triggers a submit.如果Form组件代表一个实际的 HTML 表单元素,那么您需要防止按钮触发提交时的默认行为。 Otherwise the page will immediately reload, which would explain your disappearing element.否则页面将立即重新加载,这将解释您消失的元素。

handleSend(event) {
    event.preventDefault();
    ...
};

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

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