我有一个项目,我使用Formik和react- codemirror2,我想在Formik中控制onChange,但codemirror2中的onChange没有event...and,我不知道如何使用……让我解释得更清楚:我有一个表格:
<Formik
enableReinitialize
onSubmit={values =>
{this.submitFormDefinition(values)}}
initialValues={this.state}
>
{({ handleSubmit }) => (
<div>
<Form
onSubmit={handleSubmit}
autoComplete="off"
onChange={event => {
this.handleChange(event)
}}
>
<Field
component={CustomInputComponent}
name="name"
id="id"
multiline
/>
</Form>
</div>
)}
其中我有我的函数handleChange:
handleChange = event => {console.log('change') ....}其中CustomInputComponent是我的codemirror2组件:
const CustomInputComponent = ({ field, form, ...props }) => {
return (
<CodeMirror
id={props.id}
name={props.name}
value={this.state.value}
options={{
mode: 'javascript',
theme: 'default',
lineNumbers: true,
}}
{...props}
onBeforeChange={(editor, data, value) => {
console.log({ value })
this.setState({ jsonSchema: value })
}}
onChange={(editor, data, value) => {
?????????????
}}
/>
)
}如果我使用另一个组件作为textField从Materia-UI它工作,我不需要调用我的CustomInputComponent的onChange,这是由formink直接调用…因为textField的onChange也有作为参数的事件...但正如您在代码中看到的,codeMirror的onChange没有事件...
我需要调用my handleChange而不是codeMirror的onChange ...我试着这样做:
<CodeMirror
onChange=form.handleChange或使用:
<CodeMirror
onKeyUp={form.handleChange}或者:
<CodeMirror
onKeyUp={(editor, event) => {
form.handleChange(event)
}}但是我的函数handleChange永远不会调用如何在Formik中使用react-codeMirror2?是可能的吗?我怎样才能理解福敏克的onChange?
发布于 2019-10-01 02:57:25
Formik的handleChange方法需要一个HTMLInput更改事件,并从输入中获取更新值。不幸的是,如果您使用的是CodeMirror,就不会得到这样的便利。但是,CodeMirror提供了组件onBeforeChange的值,Formik提供了一个setFieldValue方法,该方法允许您将一个值设置为状态。所以你可以这样做:
<Formik {...whateverPropsYouSet}>
(formikProps) => (
<CodeMirror
value={formikProps.values.yourDataField}
onBeforeChange={(_editor, _data, value) => {
formikProps.setFieldValue('yourDataField', value);
}
/>
)
</Formik>https://stackoverflow.com/questions/55538346
复制相似问题