最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Autosubmit ReactJS form as soon as it is created - Stack Overflow

programmeradmin5浏览0评论

Is there any way of autosubmitting a form as soon as it is created? I have a ponent <ShowVideo />. Inside this ponent, I'm dispatching an action which will give some hidden field and signatures. Afterwards, I'm rendering this form on the page. What I want is to submit this form dynamically once my form is rendered. There can be multiple forms.

One approach which came to my mind was to call another function handleSubmit after form tag, which will have form.submit() method inside it but how will I make sure that the entire form is rendered or not.

class ShowVideo extends Component {
  render() {
    let renderInputFields = null
    if (this.props.videoData.data.hasOwnProperty("signature")) {
      renderInputFields = Object.keys(launchData).map((key, i) => {
        return (<input type="hidden" name={key} key={i} value={launchData[key]} />)
      })
    }
    return (
      <div>
        <iframe width="100%" height="430px" frameBorder="0" name="videoIframe"></iframe>
        <form name="ltiLaunchForm" id="ltiLaunchForm" method="post" target="target_iframe" encType="application/x-www-form-urlencoded" action={launchUrl}>
          {renderInputFields}
          <input type="hidden" name="oauth_signature" value={signature} />
        </form>
      </div>
    )
  }
}

Is there any way of autosubmitting a form as soon as it is created? I have a ponent <ShowVideo />. Inside this ponent, I'm dispatching an action which will give some hidden field and signatures. Afterwards, I'm rendering this form on the page. What I want is to submit this form dynamically once my form is rendered. There can be multiple forms.

One approach which came to my mind was to call another function handleSubmit after form tag, which will have form.submit() method inside it but how will I make sure that the entire form is rendered or not.

class ShowVideo extends Component {
  render() {
    let renderInputFields = null
    if (this.props.videoData.data.hasOwnProperty("signature")) {
      renderInputFields = Object.keys(launchData).map((key, i) => {
        return (<input type="hidden" name={key} key={i} value={launchData[key]} />)
      })
    }
    return (
      <div>
        <iframe width="100%" height="430px" frameBorder="0" name="videoIframe"></iframe>
        <form name="ltiLaunchForm" id="ltiLaunchForm" method="post" target="target_iframe" encType="application/x-www-form-urlencoded" action={launchUrl}>
          {renderInputFields}
          <input type="hidden" name="oauth_signature" value={signature} />
        </form>
      </div>
    )
  }
}
Share Improve this question edited Nov 23, 2018 at 20:51 vijayscode asked Nov 23, 2018 at 20:43 vijayscodevijayscode 2,0155 gold badges24 silver badges38 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 11

A more contemporary solution (using React Hooks) might look like:

import React, { useEffect, useRef } from "react";

const AutoSubmitForm = ({
  actionUrl,
  params
}) => {
  const formRef = useRef(null);
  useEffect(() => {
    formRef.current.submit();
  }, []);

  return (
    <form ref={formRef} method="POST" action={actionUrl}>
      {Object.keys(params).map(name => (
        <input
          type="hidden"
          name={name}
          value={params[name]}
        ></input>
      ))}
    </form>
  );
};

If you want to auto submit the form after it's been created but the form has some of its elements rendered later, then i remend adding a ponentDidUpdate lifecycle function that will dynamically trigger after a new rerender.

In this function we will detect if the rerender has rendered all of the form elements. If so, then we can programmatically trigger the form submit.

ponentDidUpdate() {
    const formElementKey1Exists = document.getElementById("element1");
    const formElementKey2Exists = document.getElementById("element2");
    ...
    if (formElementKey1Exists  && formElementKey2Exists && ...) {
        document.getElementById('ltiLaunchForm').submit();
    }
}

The conditional will ensure your entire form is rendered before dynamically submitting.

发布评论

评论列表(0)

  1. 暂无评论