我尝试使用antd的最后一个版本(3.10.0)和react(16.5.2)。
根据https://reactjs.org/docs/refs-and-the-dom.html,我使用了新的引用方式
this.myRef = React.createRef();当它裂开时,应该喜欢:
rend(){
<Select style={{ width: 200 }} ref={this.myRef}>
{Object.entries(this.state.catedict)
.map(en => <Option key={en[0]}>{en[1]}</Option>)}
</Select>
}但是当我想得到输入或选择的值时
我试着:
console.log(this.myRef.current.value);但只能得到错误的结果。
我甚至发现:
console.log(this.myRef.current);结果是:
t {props: {…}, context: {…}, refs: {…}, updater: {…}, saveSelect: ƒ, …}
context: {}
props: {style: {…}, children: Array(2), prefixCls: "ant-select", showSearch: false, transitionName: "slide-up", …}
rcSelect: t {props: {…}, context: {…}, refs: {…}, updater: {…}, onInputChange: ƒ, …}
refs: {}
renderSelect: ƒ (n)
saveSelect: ƒ (n)
state: null
updater: {isMounted: ƒ, enqueueSetState: ƒ, enqueueReplaceState: ƒ, enqueueForceUpdate: ƒ}
_reactInternalFiber: Na {tag: 2, key: null, type: ƒ, stateNode: t, return: Na, …}
__proto__: v我要给出选择的价值。我该怎么办?
发布于 2018-10-21 14:08:48
在每次更改时保存Select的值!你想用的时候就用。
<Select style={{ width: 200 }} onChange={(value)=>{
this.selectValue = value;
}}>并在其他地方使用:
console.log('Select Value', this.selectValue)发布于 2019-03-16 04:10:14
您可以通过ref获得它,antd选择是rc-select的特殊选项,如果您想获得值,您仍然可以通过ref.rcSelect获得它。
`the react dom`
<Select ref={r => this.ctryListRef = r} />
`the js code`
console.log(this.ctryListRef.rcSelect.state.value)通过rcSelect.state.value,您可以得到值。
此外,您还可以得到antd值,这只是另一个特殊的~
https://stackoverflow.com/questions/52726243
复制相似问题