我在我的React应用程序中使用Formik库。最初,我在构造函数中声明了状态值,在componentDidMount中,我使用该应用程序响应调用API来更新状态值,有人可以帮助我将状态值传递给fomik初始值
在我的情况下,formik采用了在构造函数中声明的初始状态值。
class Dropdown extends Component {
constructor(props) {
super(props);
this.state = {
//some data
};
}
componentDidMount() {
//api call
this.setState( {
//update state values with api response data
});
}
render() {
return (
<Formik
initialValues={this.state}
validate={validate(validationSchema)}
onSubmit={onSubmit}
render={
({
values,
errors,
touched,
status,
dirty,
handleChange,
handleBlur,
handleSubmit,
isSubmitting,
isValid,
handleReset,
setTouched
}) => (
//form uses initialValues
)} />
)
}
}
Run Code Online (Sandbox Code Playgroud) 如何使用正则表达式避免字符串中的某些单词
字符串不应包含测试,使用以下表达式的示例单词
^((?!(sample|test)).)*$
Run Code Online (Sandbox Code Playgroud)
我可以做,但在某些情况下会失败
example:
1) this is a test case
2) this is a testing area
Run Code Online (Sandbox Code Playgroud)
在上述两个示例案例中
1.它有一个单词测试,所以它工作得很好,但在示例
2)它没有一个应该允许的单词测试
有什么办法可以实现这一目标吗?感谢您的宝贵时间和回复