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. :)
Czy pracujesz z flux lub refluks? Obecnie pracuję z refluksem i używam tego do tego celu. – dobleUber
@melaspelas: Nie, nie używam topnika. prosty reactjs –
czy wypróbowałeś https://facebook.github.io/react/tips/communicate-between-components.html ?? – dobleUber