简体   繁体   English

提交关于道具价值改变的表格

[英]Submit form on prop value changing

I am new to React and trying to modify this application: https://github.com/nice-table/bitmex-scaled-orders我是 React 的新手并试图修改这个应用程序: https : //github.com/nice-table/bitmex-scaled-orders

My goal is, say the prop "instrumentData" found in "src/modules/orders/OrderForm.js" has "instrumentData.lastprice" value changing to a specific value in real-time in the backend.我的目标是,假设在“src/modules/orders/OrderForm.js”中找到的道具“instrumentData”的“instrumentData.lastprice”值在后端实时更改为特定值。 I want to submit the form on that page if the value reaches a specific value.如果值达到特定值,我想在该页面上提交表单。 In other words, I want to keep monitoring that prop untill it hits a number and it will submit the form upon that.换句话说,我想继续监视该道具,直到它达到一个数字,然后它才会提交表单。 Is that doable through states?这可以通过状态实现吗? I tried to research it but given I am new to React I am a bit lost as to what code to use and where exactly to add it.我试图研究它,但鉴于我是 React 的新手,我对使用什么代码以及在哪里添加它有点迷茫。

Thanks.谢谢。

Autosubmitting is simple自动提交很简单

It's simple to run some action on data change.对数据更改运行一些操作很简单。 React components are data driven - autoupdating. React 组件是数据驱动的 - 自动更新。 You can just insert a function 'into data flow'.您可以将一个函数插入到“数据流”中。

Your data source is in DataContext then you should use <DataContext.Consumer /> to get data 'stream' - stream because it's frequently updated using socket connection.您的数据源位于DataContext那么您应该使用<DataContext.Consumer />来获取数据“流” - 流,因为它经常使用套接字连接进行更新。

<DataContext.Consumer>
  { (data, submitForm, isSubmitting) => {

    console.log("context data", data );
    // extract data from `data` object
    // const someData = data.someProperty;
    // if( someData > 12345 ) {
    //   if( !isSubmitting ) {
    //     submitForm() 
    //   }
    //   return "Limit reached"
    // }
    // return null

  }}
</ DataContext.Consumer>

This snippet can be placed almost anywhere after this code:此代码段几乎可以放置在此代码之后的任何位置:

  render={({
    values,
    errors,
    touched,
    setFieldValue,
    submitForm,
    isSubmitting,
    isValid,
    validateForm
  }) => (
    <React.Fragment>
       // place it here

... and of course before end of this fragment ( </ React.Fragment> ). ...当然在这个片段结束之前( </ React.Fragment> )。

You can pass and use almost all functions defined in this component (file), fe setFieldValue("priceUpper", to update form value before submitting.您可以传递和使用此组件(文件)中定义的几乎所有函数,fe setFieldValue("priceUpper",在提交之前更新表单值。

Autosubmitting is NOT so simple自动提交没那么简单

Problem is not trivial.问题不小。 You should create a component with internal logic to:您应该创建一个具有内部逻辑的组件来:

  • set limit (render input, onChange handler, useState) instead of hardcoded value设置限制(渲染输入、onChange 处理程序、useState)而不是硬编码值
  • block(or not? checkbox?) autosubmitting in a loop (formik will submit but later it will clear isSubmitting flag - our component will autosubmit again)阻止(或不?复选框?)在循环中自动提交(formik 将提交但稍后将清除isSubmitting标志 - 我们的组件将再次自动提交)
  • render context consumer inside - optimize rerenderings在内部渲染上下文消费者 - 优化重新渲染
  • etc.等等。

Good luck ;)祝你好运 ;)

React has a really good write up on forms and handling onChange() which is an event that can fire when a field is changed https://reactjs.org/docs/forms.html . React 在表单和处理 onChange() 方面有非常好的文章,这是一个可以在字段更改时触发的事件https://reactjs.org/docs/forms.html

class NameForm extends React.Component {
  constructor(props) {
    super(props);
    this.state = {value: ''};

    this.handleChange = this.handleChange.bind(this);
  }

  submit = () => {
      // submit form
      // eg axios.post(URL, {
      //   value: this.state.value
      // })
  }

  handleChange(event) {
    this.setState({value: event.target.value});
    if (this.state.value == 10) {
       this.submit()
    }
  }

    return (
      <form>
        <label>
          Name:
          <input type="text" value={this.state.value} onChange={this.handleChange} />
        </label>
      </form>
    );
  }
}

I see the github repo is using hooks in lieu of component classes.我看到 github 存储库正在使用钩子代替组件类。
You can read about hooks here https://reactjs.org/docs/hooks-intro.html .您可以在https://reactjs.org/docs/hooks-intro.html阅读有关钩子的信息。 onChange can still call handle change and instead of this.state you may be using useState onChange 仍然可以调用句柄更改,而不是this.state您可能正在使用useState

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

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