代码之家  ›  专栏  ›  技术社区  ›  Sam

如何允许在呈现的reactjs组件中更改输入?我是否提升状态?

  •  0
  • Sam  · 技术社区  · 8 年前

    我设法得到了价值观 Hello World 在页面上呈现,但截至目前,无法修改。当我单击它时,文本光标会出现,但无法识别任何键盘输入。我该怎么做才能接收输入(如删除字符或添加字符)?

    Screenshot

    formatoc。js公司:

    {...
    var props = {
        'name' : 'form',
        'timer' : 1500,
        'callback' : function(id, validity, value) {console.log(id, validity, value);},
        'values': ["hello", "world"],
    
        'node' : new FormatOC.ArrayNode({"__array__":"unique", "__type__":"string","__minimum__":1,"__maximum__":200,"__component__":"Input"})
    }
    
    React.render(React.createElement(ArrayNodeComponent, props), document.getElementById('react-component')); 
    ...}
    

    Array\u节点。jsx公司:

     {...
        childChange: function(name, valid, value) {
            // update state
    
            this.state.values[name] = value;
            this.setState(this.state);
    
            console.log(name,value);
    
            // Call parent callback
            this.props.callback(
                this.props.name,
                this.props.newParent.valid(this.state.values),
                this.state.values
            );
        },
    
            render: function() {
                var that = this;
                console.log("Hello World");
    
                return (
                    <div id = "form">
                    {this.props.values.map(function(v, i) {
                        return (
                            <div>
                            {(that.props.node.get().constructor.name === "Parent") ?
                            <ParentComponent
                                name={that.props.name + i}
                                key={i}
                                timer={that.props.timer}
                                callback={that.childChange}
                                values={v}
                                newParent={that.props.node.get()}
                            />
                            :
                            <NodeComponent
                                name={that.props.name + i}
                                key={i}
                                timer={that.props.timer}
                                callback={that.childChange}
                                value={v}
                                newNode={that.props.node.get()}
                            />
                            }
                            </div>
                        )
                    })}
                    </div>
               )
            }
      ...}
    

    节点。jsx公司:

    onChange: function(event) {
      that = this;
    
      var event2 = event.target;
    
      if (this.state.component === "Input") {
    
        if (this.timer !== null) {
          clearTimeout(this.timer);
        };
        this.timer = setTimeout(function(){
            that.state.value = event2.value;
    
            that.props.callback(that.props.name, that.props.newNode.valid(that.state.value), that.state.value);
    
        }, this.props.timer);
    

    更新:当我尝试为字段提供任何文本输入时,控制台会出现以下错误:

    Uncaught TypeError: Cannot read property 'valid' of undefined at Constructor.childChange (array_node.js:64) at Node.js:36

    1 回复  |  直到 8 年前
        1
  •  1
  •   Justin Pearce    8 年前

    有两件事:

    首先: childChange:函数(名称、有效值、值){ //更新状态

        this.state.values[name] = value; //<- This is a no no
        this.setState(this.state);
    
    ...
    

    你在直接变异 state ,这是一个大问题。直接改变状态可能会导致意外的副作用。这就是为什么 setState() 方法您应该这样设置状态:

    this.setState({values: value});
    

    传递包含要执行的变异的对象。

    第二个: 你没有通过这个 newParent 道具中的任何物品。

    正如评论所指出的,你应该 lifting state 而不是试图将某种家长/孩子的沟通整合在一起。