Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How the get refs antd componement Select value of antd?

I try use last version of antd(3.10.0) and react(16.5.2).

I use the new way of ref according to https://reactjs.org/docs/refs-and-the-dom.html

this.myRef = React.createRef();

when it rend,should like:

rend(){
                    <Select style={{ width: 200 }} ref={this.myRef}>
                    {Object.entries(this.state.catedict)
                        .map(en => <Option key={en[0]}>{en[1]}</Option>)}
                </Select>
}

But when I want to get the value of Input or Select

I try to:

console.log(this.myRef.current.value);

But only get wrong result.

I find even:

console.log(this.myRef.current);

the result is:

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

I want give the value of Select. How should I do?

like image 653
user504909 Avatar asked Sep 15 '25 04:09

user504909


1 Answers

You can get it by the ref,the antd select is hoc of rc-select,if you want to get the value,you still get it by ref.rcSelect

`the react dom`
<Select ref={r => this.ctryListRef = r} />

`the js code`
console.log(this.ctryListRef.rcSelect.state.value)

by the rcSelect.state.value, you can get the value.

In addition, you can get antd textArea value, it's just another hoc~

like image 124
user2248202 Avatar answered Sep 16 '25 19:09

user2248202