javascript - 关于一段react代码片段 有点疑问

查看:124
本文介绍了javascript - 关于一段react代码片段 有点疑问的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问 题

import React, { Component, PropTypes } from 'react';
import { browserHistory } from 'react-router';

class SearchGithub extends Component {
    static PropTypes = {
        history: PropTypes.object.isRequired
    }
    getRef(ref){
        this.usernameRef = ref;
        console.log(ref);
    }
    handleSubmit(event){
        const username = this.usernameRef.value;
        this.usernameRef.value = '';

        const path = `/profile/${username}`;
        browserHistory.push(path)

    }
    render(){
        return (
            <div className="col-sm-12">
                <form onSubmit={() => this.handleSubmit()}>
                    <div className="form-group col-sm-7">
                        <input type="text" className="form-control" ref={(ref) => this.getRef(ref)} />
                    </div>
                    <div className="form-group col-sm-5">
                        <button type="submit" className="btn btn-block btn-primary">搜索 Github</button>
                    </div>
                </form>
            </div>
        )
    }
}

export default SearchGithub;

这是一个搜索组件,上面的:

getRef(ref){
        this.usernameRef = ref;
        console.log(ref);
    }

函数与下面的

<input type="text" className="form-control" ref={(ref) => this.getRef(ref)} />
这是什么意思啊 大家有知道的或者这么用的吗

解决方案

https://facebook.github.io/re...

这篇关于javascript - 关于一段react代码片段 有点疑问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆