gpt4 book ai didi

javascript - React 中的表单提交

转载 作者:行者123 更新时间:2023-12-03 00:40:53 27 4
gpt4 key购买 nike

我正在尝试在 React 中提交表单。该表单是连接的,因此表单数据将作为 props 出现。我想要实现的目标是,当 props 不同 时,表单应该自动提交,这意味着对于一组 props,表单应该只自动提交一次。

我尝试在 componentWillReceiveProps 中添加一个条件,我也可以在其中确定 Prop 是否不同,提交表单。最初,this.propscomponentWillReceiveProps

的第一次渲染中未定义
class App extends React.Component {
constructor(props) {
super(props)
this.state = { formSubmitted: false }
}
componentWillReceiveProps(nextProps) {
if (this.props.picmonicVideoData.data.signature !== nextProps.picmonicVideoData.data.signature) {
this.setState({formSubmitted: false});
}
componentDidUpdate = () => {
if (!this.state.formSubmitted) {
this.setState({ formSubmitted: true })
document.getElementById('ltiLaunchForm').submit()
}
}
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>
)
}
}

如有任何帮助,我们将不胜感激。

最佳答案

我建议仅使用 componentDidUpdate,因为您可以轻松比较 prevProps 和当前 props 并执行提交操作。所以你的代码看起来像这样:

componentDidUpdate = (prevProps) => {
if (this.props !== prevProps) {
document.getElementById('ltiLaunchForm').submit()
}
}

如果您计划使用 React 16.3 及更高版本,您将不再需要 componentWillReceiveProps 并且避免使用 React 中已弃用的生命周期。详细解释请看这个link .

关于javascript - React 中的表单提交,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53471456/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com