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?

阅读 9.5k
2 个回答
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>
    );
  }
}

入坑姿势不正确,你的方式是典型的命令式,问题与antd没有关系,还是得从react基础搞起,State Controlled Components

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题