我收到错误 _this.form.getValues is not a function in 6.6.0
原因ref={form => this.form = form}
。在extreact-6.6.0中,表单变量不是精确的formpanel
。所以为此你需要像这样访问
ref={form => this.form = (this.form || form.cmp)}}
另一种使用button.up('formpanel')获取formpanel
组件的方法。这个按钮是你的第一个参数handler
。
button.up('formpanel').getValues()
你可以在这里检查工作小提琴。
代码片段
import React, { Component } from 'react';
import {launch} from '@sencha/ext-react';
import { ExtReact } from '@sencha/ext-react';
import { Container, Label, FormPanel, TextField, Button } from '@sencha/ext-modern';
class App extends Component {
state = {
values:JSON.stringify({
fname: 'null',
lname: 'null'
})
}
submit = (btn) => {
const values = btn.up('formpanel').getValues();
console.log('Values using up selector',values);
console.log('Values using up ref',this.form.getValues());
this.setState({values:JSON.stringify(this.form.getValues())});
}
render() {
return (
<Container defaults={{ margin: 10, shadow: true }}>
<FormPanel title="Form" ref={form => this.form = (this.form || form.cmp)}>
<TextField name="fname" label="First Name"/>
<TextField name="lname" label="Last Name"/>
<Button handler={this.submit} text="Submit"/>
</FormPanel>
<Label padding={'10'} html={this.state.values} />
</Container>
)
}
}
launch(<ExtReact><App /></ExtReact>);