Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.9k views
in Technique[技术] by (71.8m points)

ant design 如何从 Button 组件的回调函数调用和修改 Input 的字符串?

import React from 'react';
import { Input, Select, Button, Icon } from 'antd';

class Example extends React.Component {
  handleClick() {
    this.input.value = 'This is TextBox';
  }
  render() {
    return (
      <div>
        {/* <TextBox ref={ref => this.textBox = ref}/> */}
        <Input ref={ref => this.input = ref} />
        <Button onClick={this.handleClick.bind(this)}>搜索</Button>
      </div>
    );
  }
}
export default Example;

如果是用 ant-design 的 Input 和 Button,上面的 handleClick 函数不能改变 Input 的值。
但是如果用的原生的 input 上面的函数是可以改变的。
请问其他的组件标签如何获取到 ant-design 的 Input?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
class Example extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      value: ''
    }
  }
  handleChange(e) {
    this.setState({
        value: e.target.value
    })
  }
  handleClick() {
    this.setState({
        value: 'This is TextBox'
    })
  }
  render() {
    return (
      <div>
        <Input value={this.state.value} onChange={(e) => this.handleChange(e)} />
        <Button onClick={() => this.handleClick()}>搜索</Button>
      </div>
    );
  }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...