Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.8k 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 Answer

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>
    );
  }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...