开发者社区> 问答> 正文

#React 如何创建裁判?

#React 如何创建裁判?

展开
收起
因为相信,所以看见。 2020-05-07 16:29:29 840 0
1 条回答
写回答
取消 提交回答
  • 阿里,我所有的向往

    有两种方法

    这是最近添加的方法。参考文献使用创建的React.createRef()方法和连接反应经由元素ref属性。为了在整个组件中使用引用,只需将引用分配给构造函数中的instance属性。

    class MyComponent extends React.Component {
      constructor(props) {
        super(props)
        this.myRef = React.createRef()
      }
      render() {
        return <div ref={this.myRef} />
      }
    }
    
    

    无论React版本如何,您也可以使用ref回调方法。例如,按以下方式访问搜索栏组件的输入元素

    class SearchBar extends Component {
       constructor(props) {
          super(props);
          this.txtSearch = null;
          this.state = { term: '' };
          this.setInputSearchRef = e => {
             this.txtSearch = e;
          }
       }
       onInputChange(event) {
          this.setState({ term: this.txtSearch.value });
       }
       render() {
          return (
             <input
                value={this.state.term}
                onChange={this.onInputChange.bind(this)}
                ref={this.setInputSearchRef} />
          );
       }
    }
    

    您还可以使用闭包在功能组件中使用引用。 注意:即使不推荐这样做,也可以使用内联引用回调

    2020-05-07 16:31:50
    赞同 展开评论 打赏
问答分类:
问答标签:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
利用编译将 Vue 组件转成 React 组件 立即下载
React在大型后台管理项目中的工程实践 立即下载
React在大型后台管理项目中的工程实践 立即下载