http://codepen.io/anon/pen/KNEzOX?editors=0010Anruf eine andere Komponente in react.js
Ich habe die Komponente A zu machen, dass die nicht holen und die Liste machen. Ich habe Komponente B, die die Benutzereingabe akzeptieren. Wie feuere ich Komponente A von Komponente B? Ich denke, meine Struktur ist falsch.
const Profiles = React.createClass({
getInitialState(){
return {profiles: []}
if(!this.props.username){
return false;
}
fetch('https://api.github.com/search/users?q=' + this.props.username)
.then(function(response) {
return response.json()
}).then(function(json) {
this.setState({profile: json.items})
})
},
render(){
return (
<ul>
{
this.state.profiles.map(profile =>
<li key={profile.id}>profile.name</li>
)
}
</ul>
)
}
})
const Input = React.createClass({
getInitialState(){
return {username:''}
},
handleInputChange(e){
this.setState({username:e.target.value})
},
handleSearch(){
if(!this.state.username){
alert('username cannot be empty');
return false;
}
//call profile component and pass username to it?
},
render() {
return(
<div>
<input type="text" placeholder="github username" onChange={this.handleInputChange} value={this.state.username} />
<button onClick={this.handleSearch}>Search</button>
<Profiles username={this.state.username} />
</div>
)
}
});
Ich habe Ihren ersten Punkt, das behoben. Aber ich verstehe deine zweite nicht. Ich weiß nicht, wie ich die Profilkomponente von der Eingabekomponente aus aufrufen soll. –
Ich habe einen Code zur Erklärung hinzugefügt. – Scimonster
Ich denke meine Struktur ist falsch. Ich sollte nicht in Komponente A holen, nur holen Sie innerhalb des Click-Handlers, dann übergeben Sie das Ergebnis als Prop zu Komponente B, die das Rendern tun wird. –