2015-03-25 81 views
0

我有以下反应脚本,它可以正常工作。然而,我正在学习反应和同行向我提到这件事: -从reactjs组件中移除状态

一个改进将是创建另一个组件,从 道具呈现。状态通常会被忽视,除非需要,状态通常是 在其组件的根部使用道具保存。更多道具 ==不错。

我很努力地完全掌握他的意见,在google上无法找到任何简洁的内容来重写这段代码。任何人都可以帮助改写更多reactjs的方式吗?

var MachineInfo = React.createClass({ 
    getInitialState: function() { 
     return { 
      data: [] 
     }; 
    }, 

    componentDidMount: function() { 
     $.get(this.props.source, function(result) { 

      this.setState({ 
       data: result 
      }); 


     }.bind(this)); 
    }, 

    render: function() { 

     var createItem = function(item) { 
      return <p>{item.Id} {item.Key} {item.Value} </p>; 
     }; 

     return <div>{this.state.data.map(createItem)}</div>; 

    } 
}); 

React.render(
    <MachineInfo source="/ajax/namevalues/2" />, 
    document.getElementById('reactdiv') 
); 

这将JSON我检索

[ 
    {"Id":5,"Key":"Temp","Value":"160"}, 
    {"Id":6,"Key":"Pressure","Value":"Light"}, 
    {"Id":7,"Key":"Time","Value":"Pre 10 Press 20"}, 
    {"Id":8,"Key":"Release","Value":"Warm"} 
] 

回答

1

您可以创建另一个组件像MachineInfoItem,并通过这将使使用道具像下面的JSON。

var MachineInfo = React.createClass({ 
    getInitialState: function() { 
     return { 
      data: [] 
     }; 
    }, 

    componentDidMount: function() { 
     $.get(this.props.source, function(result) { 

      this.setState({ 
       data: result 
      }); 


     }.bind(this)); 
    }, 

    render: function() { 

     var createMachineInfoItems = function(info) { 
      return <MachineInfoItem info={info} /> 
     }; 

     return <div> 
      {this.state.data.map(createMachineInfoItems)} 
     </div>; 

    } 
}); 

var MachineInfoItem = React.createClass({ 
    render: function() { 
    return <p> 
     {this.props.info.Id} 
     {this.props.info.Key} 
     {this.props.info.Value} 
    </p> 
    } 
}); 

React.render(
    <MachineInfo source="/ajax/namevalues/2" />, 
    document.getElementById('reactdiv') 
); 

Thinking in React是一个很好的博客文章,它解释了这一点。

+0

啊我现在从代码中看到,它可以填充返回的json到状态。这是对的。他的评论是关于呈现对象列表的。即创建另一个组件。 – Rippo 2015-03-25 07:59:11

+0

已经标记为答案,因为它是有道理的。然而,我也发布了一个稍微更精致的实例,其中有三个组件,一个用于获取状态,一个用于标题,一个用于项目 – Rippo 2015-03-25 08:58:57

0

感谢@Deepak为我展示了我需要知道的答案。我最终使用的代码是: -

var MachineInfo = React.createClass({ 
    getInitialState: function() { 
     return { 
      data: [] 
     }; 
    }, 

    componentDidMount: function() { 
     $.get(this.props.source, function(result) { 
      this.setState({ 
       data: result 
      }); 
     }.bind(this)); 
    }, 

    render: function() { 
     return <MachineInfoHeader data={this.state.data} />  
    } 
}); 

var MachineInfoHeader = React.createClass({ 
    render: function() { 
     var createMachineInfoItems = function(info) { 
      return <MachineInfoItem info={info} /> 
     }; 
     return <div>{this.props.data.map(createMachineInfoItems)}</div> 
    } 
}); 

var MachineInfoItem = React.createClass({ 
    render: function() { 
     return <p> 
      {this.props.info.Id} 
      {this.props.info.Key} 
      {this.props.info.Value} 
     </p> 
    } 
}); 

React.render(
    <MachineInfo source="/ajax/namevalues/2" />, 
    document.getElementById('reactdiv') 
); 
+0

若要downvoter,请指出您为什么downvoted?我接受了迪帕克的回答,并给了他一个+1,因为他让我朝着正确的方向前进。我的改进将在未来帮助人们。 – Rippo 2015-03-25 14:59:50