gpt4 book ai didi

javascript - 在 React Native 中自动提交 Redux 表单

转载 作者:行者123 更新时间:2023-11-28 14:56:12 24 4
gpt4 key购买 nike

有人知道如何在满足某些条件时在 React Native 中自动提交 Redux 表单吗?我在下面的尝试发出了警告。

在文档中有一个 remote submitting 的示例,但它使用 HTML 表单的 <form onSubmit={handleSubmit}> 。在 React Native 中相当于什么?

我的尝试:当表单的输入长度>=2时提交

class MyClass extends React.Component {
constructor(props) {
super(props);
this.handlSubmitWrapper = this.handlSubmitWrapper.bind(this);
}

handlSubmitWrapper() {
const { handleSubmit } = this.props;
handleSubmit(() => console.log('submitting...'))();
}

getTextInput({ input: { onChange, value, ...otherProps }, autoSubmit }) {
if (value && value.length > 1) {
// triger submit
autoSubmit();
}

return (
<TextInput
onChangeText={onChange}
style={{height: 50, backgroundColor: '#666'}}
{...otherProps}
maxLength={2}
/>
);
}

render() {
return (
<View>
<Field name="myText"
component={this.getTextInput}
autoSubmit={this.handlSubmitWrapper}
/>
</View>
);
}
}

const MyForm = reduxForm({
form: 'setupPasscode',
})(MyClass);

export default connect()(MyForm);

警告:


ExceptionsManager.js:71 Warning: setState(...): Cannot update during an existing state transition (such as within `render` or another component's constructor). Render methods should be a pure function of props and state; constructor side-effects are an anti-pattern, but can be moved to `componentWillMount`.

最佳答案

您在渲染组件时调用提交操作。你不能用 react 来做到这一点。您应该使用 TextInput onChange 方法来实现这一点。

class MyClass extends React.Component {
constructor(props) {
super(props);
this.handlSubmitWrapper = this.handlSubmitWrapper.bind(this);
this.handleInputChange = this.handleInputChange.bind(this);
}

handlSubmitWrapper() {
const { handleSubmit } = this.props;
handleSubmit(() => console.log('submitting...'))();
}

handleInputChange(event) {
const newText = event.nativeEvent.text;
if (newText && newText.length > 1) {
// triger submit
this.handlSubmitWrapper();
}
}

getTextInput({ input: { onChange, value, ...otherProps } }) {
return (
<TextInput
onChange={this.handleInputChange}
onChangeText={onChange}
style={{height: 50, backgroundColor: '#666'}}
{...otherProps}
maxLength={2}
/>
);
}

render() {
return (
<View>
<Field name="myText" component={this.getTextInput} />
</View>
);
}
}

const MyForm = reduxForm({
form: 'setupPasscode',
})(MyClass);

export default connect()(MyForm);

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

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