React JS - 在另一个组件中渲染一个组件

时间:2017-08-18 02:17:08

标签: javascript reactjs

我是React的新手,所以请原谅我这里的邋。我有几个api的异步调用,我试图从另一个组件中显示返回的数据。

这是第一个对api进行异步调用的组件:

      var Program = React.createClass({
      getInitialState: function(){
          return{
              data: []
          };
      },
      componentDidMount(){
          this.getDataFromServer('https://api.myjson.com/bins/5bdb3');
      },
      displayResponse: function(response) {
        this.setState({
          data: response
        })
      },
      getDataFromServer: function(url){
          $.ajax({
              type: 'GET',
              dataType: 'json',
              url: url,
              success: function(response, i) {
                this.displayResponse(response)
              }.bind(this)
          });
      },
      render: function() {
        var size = 3; // only show top 3 results
        var list = this.state.data;
        var result = list.slice(0, size).map(function(result, i) {
          return <ProgramItem key={ i } course={ result } index={ i } />
        });
        return (
          <div>{ result }</div>
        );
      }
  });

  var ProgramItem = React.createClass({
    render: function() {
      var course = this.props.course;
      return (
        <div className="charts-block">
          <div className="top-section">
            <div className="charts-block-title">
              <p className="title">{ course.Name }</p>
              <button className="edit-ico"><i className="fa fa-pencil"></i></button>
            </div>
            <p>Sales by month</p>
            <div className="charts">
              <img src="images/graph.png" alt="Stats Chart" />
            </div>
          </div>
          <div className="stats-list-wrapper parent-show-more">
            <table className="stats-table header">
              <tr>
                <th colSpan={ 2 } style={{ textAlign: 'right' }}>Current</th>
                <th>1 - Year</th>
              </tr>
              <tr>
                <td>Total Monlthy Sales</td>
                <td className="monthly-sales">{ course.TotalMonthlySales }</td>
                <td><img src="images/spark_line.png" alt="Stats Spark Line" /></td>
              </tr>
            </table>
            <div className="show-more-table">
              <div className="stats-table plans-table">
                <div>
                  <div>Price Name</div>
                  <div>Current</div>
                  <div>1 - Year</div>
                </div>
                <div className="planInfo">-- Data From other Component here --</div>
              </div>
            </div>
            <div className="click-more-row">
              <span>more</span>
            </div>
          </div>
        </div>
      );
    }
  });

<div class="planIfno">内部我希望&#34;注入&#34;来自第二个组件的数据:

      var Plan = React.createClass({
      getInitialState: function(){
          return{
              data: []
          };
      },
      componentDidMount(){
          this.getDataFromServer('https://api.myjson.com/bins/47axv');
      },
      displayResponse: function(response) {
        this.setState({
          data: response
        })
      },
      getDataFromServer: function(url){
          $.ajax({
              type: 'GET',
              dataType: 'json',
              url: url,
              success: function(response, i) {
                this.displayResponse(response)
              }.bind(this)
          });
      },
      render: function() {
        var size = 7; // only show top 3 results
        var list = this.state.data;
        var result = list.slice(0, size).map(function(result, i) {
          return <PlanItem key={ i } plan={ result } index={ i } />
        });
        return (
          <div>{ result }</div>
        );
      }
    });

    var PlanItem = React.createClass({
      render: function() {
        var plan = this.props.plan;
        return (
          <tr>
            <td>{ plan.Name }</td>
            <td>{ plan.Sales }</td>
            <td></td>
          </tr>
        );
      }
    });

任何方向都会很受欢迎!

2 个答案:

答案 0 :(得分:1)

很难理解你在寻找什么,并且这样的事情,因为代码太多......我花了一段时间才找到planIfno [ sic]是

我认为您要找的是var Plan = React.createClass(...),您可以在代码的其他部分使用它,例如<Plan plan={{Name: "something", Sales: 10 }}/>

每个组件都将处理它们可能/可能不具有的任何异步代码。

最终看起来像

<div className="planInfo">
  {/* hardcoded plan, but you could just as easily pass in `plan={plan}` */}
  <Plan plan={{Name: "something", Sales: 10 }}/> 
</div>

答案 1 :(得分:1)

只需将<Plan />放在那里。