2015-04-28 57 views
6

我正在學習Reactjs。我已經使用rails實現了一個示例反應應用程序。我有很多搜索找到解決方案,但我沒有找到任何。我想從onClick函數中調用另一個組件。但沒有發生。這可能是我嘗試實現的目標嗎?如果是,那麼請指出我在哪裏犯錯,如果不是,那麼我可以採用哪種方式實施。這裏是我的代碼:如何從ReactJS中的onClick函數調用另一個組件

var Comment = React.createClass({ 
    render: function() { 
    return (
     <div id={"comment_"+ this.props.id }> 
     <h4>{ this.props.author } said:</h4> 
     <p>{ this.props.desc }</p> 
     <a href='' onClick={this.handleDelete}>Delete</a> | #this is for delete which works great 
     <a href='' onClick={this.handleEdit}>Edit</a> 
     # If I put here then it works fine <UpdateComments comments={ this.props} /> but I don't want it here 
     </div> 
    ) 
    }, 
    handleDelete: function(event) { 
    $.ajax({ 
     url: '/comments/'+ this.props.id, 
     type: "DELETE", 
     dataType: "json", 
     success: function (data) { 
     this.setState({ comments: data }); 
     }.bind(this) 
    }); 

    }, 
    handleEdit: function(event) { 
    var Temp = React.createClass({ 
     render: function(event){ 
     return(<div> 
      <UpdateComments comments={ this.props} /> #here I want to call UpdateComments component 
      </div> 
     ) 
     } 
    }); 
    } 
}); 

更新: 如果我嘗試下面的技巧後,它調用組件,但重新加載頁面,並再次消失調用的組件:(

handleEdit: function() { 

    React.render(<UpdateComments comments={ this.props} /> , document.getElementById('comment_'+ this.props.id)); 
} 

任何其他細節,如果你需要的話可以隨意問。提前致謝:)

+0

您是否在使用助焊劑或迴流?目前,我正在與迴流,我用商店來實現這一目標。 – dobleUber

+0

@melaspelas:不,我不使用助焊劑。簡單reactjs –

+0

你試過https://facebook.github.io/react/tips/communicate-between-components.html ?? – dobleUber

回答

4

也許這個fiddle可能指向你正確的方式

var Hello = React.createClass({ 
 
    render: function() { 
 
     return <div>Hello {this.props.name} 
 
      <First1/> 
 
      <First2/> 
 
     </div>; 
 
    } 
 
}); 
 

 
var First1 = React.createClass({ 
 
    myClick: function(){ 
 
     alert('Show 1'); 
 
     changeFirst(); 
 
    }, 
 
    render: function() { 
 
     return <a onClick={this.myClick}> Saludo</a>; 
 
    } 
 
}); 
 

 
var First2 = React.createClass({ 
 
    getInitialState: function(){ 
 
     return {myState: ''}; 
 
    }, 
 
    componentDidMount: function() { 
 
     var me = this; 
 
     window.changeFirst = function() { 
 
      me.setState({'myState': 'Hey!!!'}) 
 
      
 
     } 
 
    }, 
 
    componentWillUnmount: function() { 
 
     window.changeFirst = null; 
 
    }, 
 
    render: function() { 
 
     return <span> Saludo {this.state.myState}</span>; 
 
    } 
 
}); 
 

 
React.render(<Hello name="World" />, document.getElementById('container'));
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/0.13.1/react-with-addons.js"></script> 
 
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/0.13.1/JSXTransformer.js"></script> 
 
<script src="https://facebook.github.io/react/js/jsfiddle-integration.js"></script> 
 

 
<div id="container"> 
 
    <!-- This element's contents will be replaced with your component. --> 
 
</div>

基本上我使用這些鏈接:

communicate between components

dom event listeners

它希望這會有所幫助。

此外,您可以使用容器組件並將它用作兩個組件之間的橋樑。

相關問題