ReactJs输入元素内容不会更改

时间:2016-12-11 10:40:54

标签: reactjs events onchange

请参阅下面的代码,无论我在输入元素中输入什么内容,其值都不会改变。
似乎输入元素变为只读。 但是,onChange事件会触发,控制台日志输出始终显示初始值。 我的代码有什么问题吗?

Combo.tsx

import * as React from "react";

export interface ComboProps { label:string, value:string }

// 'HelloProps' describes the shape of props.
// State is never set so we use the 'undefined' type.
export class Combo extends React.Component<ComboProps, ComboProps> {
    constructor(props:any){
        super(props);
        this.state = {label:this.props.label, value: this.props.value};
        this.update = this.update.bind(this);
    }
    update(event:any){
        console.log(this.state.value);
    }
    render() {
        return <div className="wo-boundary">
            <div  className="wo-label" ref="label">{this.state.label}</div>
            <div  className="wo-widget wo-input-module wo-combo">
                <input ref="box" type="text" onChange={this.update} value={this.state.value} />
                <div ref="toggle"  className="toggle">
                    <svg  className="icon-dropdown-toggle">
                        <use href="svg-symbols.svg#icon-dropdown-toggle"></use>
                    </svg>
                </div>
            </div>
        </div>;
    }
}

1 个答案:

答案 0 :(得分:1)

您需要更新update(event){ this.setState({value: event.target.value}); } 回调中的状态:

#include <stdio.h>

    int power(int base, int exp) {
        int result = 1;
        int i;
        for (i = 0; i < exp; i++) {
            result *= base;
        }
        return result;
    }

    int main() {

        printf("%d is the result.\n", power(5,3));
        return 0;
    }