2015-04-28 9 views
6

Uczę się Reactjs. Zaimplementowałem jedną przykładową aplikację reagowania z szynami. Mam dużo wyszukiwań, aby znaleźć rozwiązanie, ale nie znalazłem żadnego. Chciałem zadzwonić do innego komponentu z funkcji onClick. Ale nic się nie dzieje. Czy to możliwe, co staram się osiągnąć? Jeśli tak, to proszę wskazać mi, gdzie popełniłem błąd, a jeśli nie, to w jaki sposób mogę go wprowadzić. Tu jest mój kodu:Jak wywołać inny komponent z funkcji onClick w ReactJS

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> 
     ) 
     } 
    }); 
    } 
}); 

Aktualizacja: Gdy próbuję poniżej sztuczki to nazwać komponent ale przeładować stronę i znowu znikają nazwie komponentu :(

handleEdit: function() { 

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

każdy inny szczegół jeśli ciebie wymagane, a następnie proszę zapytać. Z góry dzięki. :)

+0

Czy pracujesz z flux lub refluks? Obecnie pracuję z refluksem i używam tego do tego celu. – dobleUber

+0

@melaspelas: Nie, nie używam topnika. prosty reactjs –

+0

czy wypróbowałeś https://facebook.github.io/react/tips/communicate-between-components.html ?? – dobleUber

Odpowiedz

4

Może to fiddle może wskazać ci w odpowiedni sposób

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>

Zasadniczo używam te linki:

communicate between components

dom event listeners

Ma nadzieję, że to pomaga.

Można również użyć komponentu kontenera i użyć go jako mostu między oboma komponentami.

Powiązane problemy