React.js如何在内部组件中查找ref元素

时间:2015-12-11 23:58:04

标签: javascript reactjs

我想与<过滤/>从onChange事件到父组件的组件< ViewTwoComponent />我不知道该怎么做

您是否知道如何在组件与其父级之间共享数据

ViewTwoComponent他们没有看到ref值,我不知道为什么?

在控制台上出错:未捕获TypeError:无法读取属性' getDOMNode'未定义的

var ViewTwoComponent = React.createClass({
    "getInitialState": function() {
        return {
            "userTextValue": "hello11111111111111",
            "userTextRef": "userTextRef"
        }
    },
    "updateState": function(value) {
        this.setState({userTextValue: value })
    },
    "handleChange": function() {
        this.updateState(this.refs.userTextRef.getDOMNode().value)
    },
    "render": function() {
        return <div>
            <Inner />
            <Filtering refName={this.state.userTextRef} handleChange={this.handleChange} userTextValue={this.state.userTextValue} />
        </div>;
    }
})

var Inner = React.createClass({
    "render": function() {
        return <span>INNER</span>;
    }
});

var Filtering = React.createClass({
    "render": function() {
        return <span>
            <input type="text" ref={this.props.refName} onChange={this.props.handleChange} value={this.props.userTextValue} />
        </span>;
    }
});

React.render(< ViewTwoComponent />, document.getElementById("inner"))

2 个答案:

答案 0 :(得分:0)

当您尝试引用userTextRef时,您的意思有点令人困惑,但我假设您想要处于您所在州的价值。我还假设密钥userTextRef的值实际上不是 "handleChange": function() { this.updateState(this.refs[userTextRef]getDOMNode().value) } 。您可以尝试使用方括号来访问该值。

$start = strtotime('2015-11-25 00:00:00'); // 1448409600
$end = strtotime('2015-11-25 23:59:59'); // 1448495999

SELECT * FROM `orders` WHERE `time-placed` BETWEEN '$start' AND '$end'

答案 1 :(得分:0)

我是React的新手,但是从我记得的教程中,你应该将Filtering组件的函数传递给ViewTwoComponent

var Filtering = React.createClass({
        "childChanged" : function(child, value) {
           console.log("child: " + child + "change value to: " value);
        },
        "render": function() {
            return <span>
                <input type="text" ref={this.props.refName} onChange={this.props.handleChange} value={this.props.userTextValue} notifyParent={this.childChanged} />
            </span>;
        }
    });

然后,在handleChange课程的ViewTwoComponent方法中,您拨打this.props.notifyParent(this, this.state.userTextValue);