2016-12-16 24 views
0

我有一個名爲SearchInput成分,它有它我怎麼不能在我的React搜索框中輸入任何內容?

<input 
    type='text' 
    className='input-field' 
    value={value} 
    placeholder={this.state.placeholder} 
    // onFocus={::this.onFocus} 
    // onBlur={::this.onBlur} 
    // onKeyUp={::this.onKeyUp} 
    // onKeyDown={::this.hanleArrowKeys} 
    // onChange={::this.onChange} 
    /> 

但是,當我輸入任何東西進去,什麼也沒有發生。文本甚至沒有出現。我究竟做錯了什麼?

回答

3

可能你的狀態沒有更新,可能你沒有綁定「this」來運行onChange?

以下是在正確的輸入的示例反應:

class SearchInput extends Component { 
    constructor(props) { 
    super(props); 
    this.state = { value: '' }; 
    this.handleInputChange = this.handleInputChange.bind(this); 
    } 

    handleInputChange(event) { 
    this.setState({ value: event.target.value }); 
    } 

    render() { 
    return <input type="text" value={this.state.value} onChange={this.handleInputChange}/>; 
    } 
} 
1

可能你與在所述部件的狀態的屬性綁定在輸入框中的值和你要麼不提供的onChange丙輸入標記或onChange回調方法不會更新輸入標記值綁定到的狀態中的屬性。

相關問題