在另一个组件中反应JS引用函数 [英] React JS reference function in another component

查看:70
本文介绍了在另一个组件中反应JS引用函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过另一个组件呈现一个按钮来引用和/或影响另一个组件的状态。

I'm trying to get a button rendered through another component to reference and/or influence the state of a different component.

var Inputs = React.createClass({
  getInitialState: function(){
    return {count: 1};
  },
  add: function(){
    this.setState({
      count: this.state.count + 1
    });
  },
  render: function(){
    var items = [];
    var inputs;
      for (var i = 0; i < this.state.count; i++){
        items.push(<input type="text" name={[i]} />);
        items.push(<br />);
      }
    return (
      <div className="col-md-9">
        <form action="/" method="post" name="form1">
          {items}
          <input type="submit" className="btn btn-success" value="Submit Form" />
        </form>
      </div>
     );
   }
 });

我想编写一个能够访问Inputs中的add函数的新组件。我尝试直接用 Inputs.add 引用它,如下所示:

I want to write a new component that will be able to access the add function in Inputs. I tried to reference it directly with Inputs.add like this:

var Add = React.createClass({
  render: function(){
    return (
      <input type="button" className="btn" value="Add an Input" onClick={Inputs.add} />
    );
  }
});

但这不起作用。我如何通过另一个组件访问组件的功能,或通过另一个组件影响组件的状态?谢谢。

But that didn't work. How would I be able to access a component's functions through another component, or influence the state of a component through another component? Thanks.

推荐答案

您可以通过创建一个负责管理状态的父组件来完成此操作,然后将状态向下推将子组件作为道具。

You could accomplish this by creating a parent component that is responsible for managing the state and then just push the state down to the sub-components as props.

/** @jsx React.DOM */

var Inputs = React.createClass({

    render: function () {
        var items = [];
        var inputs;
        for (var i = 0; i < this.props.count; i++) {
            items.push( <input type="text" name={[i]} />);
            items.push(<br />);
        }
        return ( 
            <div className = "col-md-9"> 
                <form action = "/" method = "post" name = "form1"> 
                    {items} 
                    <input type="submit" className="btn btn-success" value = "Submit Form" />            
                </form>
            </div> 
       );
    }
});

var Add = React.createClass({
    render: function () {
        return (<input type = "button" className="btn" value="Add an Input" onClick={this.props.fnClick}/> );
  }
});

var Parent = React.createClass({
    getInitialState: function(){
        return {count:1}
    },
    addInput: function(){
        var newCount = this.state.count + 1;
        this.setState({count: newCount});
    },
    render: function(){
        return (
            <div>
                <Inputs count={this.state.count}></Inputs>
                <Add fnClick={this.addInput}/>
            </div>
        );
    }
});

React.renderComponent(<Parent></Parent> , document.body);

jsFiddle

这篇关于在另一个组件中反应JS引用函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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