简体   繁体   English

为什么我的 POST 请求会导致完全重新渲染?

[英]Why is my POST request causing a complete rerender?

I'm new to react and having some very curious behaviour when sending a post request to firebase, hoping someone can explain.我是新手,在向 firebase 发送帖子请求时会有一些非常奇怪的行为,希望有人能解释一下。 I have a form with two required text areas, both set respective state keys to their input value onChange.我有一个包含两个必需文本区域的表单,它们都将各自的 state 键设置为它们的输入值 onChange。 I then have a button hooked to a POST function using an axios instance.然后,我使用 axios 实例将按钮连接到 POST function。

The request goes through absolutely fine if none, or only one of the text areas has input.如果没有,或者只有一个文本区域有输入,则请求绝对可以通过。

If both text areas have input the request doesn't happen and instead I get a complete re-render/refresh of the page.如果两个文本区域都输入了请求,则不会发生请求,而是我会完全重新渲染/刷新页面。 I have tried chopping the code up many ways, adding conditionals to the POST function changing/testing the instance to see if I'm missing something, but nothing seems to work.我尝试以多种方式将代码切碎,向 POST function 添加条件来更改/测试实例以查看我是否遗漏了什么,但似乎没有任何效果。 I can't understand what is causing the rerender and stopping the request when the only change is a second input value.当唯一的变化是第二个输入值时,我无法理解是什么导致了重新渲染和停止请求。 Any help appreciated!任何帮助表示赞赏!

Here is my code:这是我的代码:

class NewPostForm extends Component {
  state = {
    title: "",
    offer: "",
    return: "",
  };

  postHandler = () => {
    const post = {
      title: this.state.title,
      offer: this.state.offer,
      return: this.state.return,
    };
      instance
        .post("/posts.json/", post)
        .then((response) => {
          console.log(response);
        })
        .catch((error) => {
          console.log(error);
        });
    }
  };
  render() {
return (
<form>
 <textarea
            key={"in0"}
            value={this.state.offer}
            onChange={(event) => this.setState({ offer: event.target.value })}
            required
          />
          <textarea
            key={"in1"}
            value={this.state.return}
            onChange={(event) => this.setState({ return: event.target.value })}
            required
          />

          <div style={{ display: "flex", alignItems: "center" }}>
            <MainButton click={this.postHandler} label={"Post"} />
            <MainButton click={this.props.click} label={"Cancel"} />
          </div>
        </form>)

MainButton def:主按钮定义:

const MainButton = (props) => { 
  return (
    <button onClick={props.click} style={style}> {props.label} </button>
  ); 
};

A button or submit input within a form can trigger the form being "submitted" to a target, which in classic HTML fashion means sending the inputs back to the server, and then loading what the server returns into a new document.表单中的按钮或提交输入可以触发表单“提交”到目标,在经典的 HTML 方式中,这意味着将输入发送回服务器,然后将服务器返回的内容加载到新文档中。 If you're doing fetch/axios/XHR/whatever instead on form button clicks, you need to prevent the default behavior through the event object.如果您在单击表单按钮时执行 fetch/axios/XHR/whatever,则需要通过事件 object 来防止默认行为。 Change your onClick handler to:将您的 onClick 处理程序更改为:

postHandler = e => {
  e.preventDefault();

  // rest of the code
};

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

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