Answer the question
In order to leave comments, you need to log in
How to pass an input value from a neighboring component to an axios request?
Can you please tell me how to pass the input value from the neighboring component to the axios request? Is it necessary that value from 'TextField' in list.js be immediately passed to 'q:' api.js? That is, so that when the field changes, a
list.js request is sent every time
constructor(){
super();
this.state = {
users: [],
value: ''
}
}
componentDidMount(){
API.getUsers().then((allUsers) => {
console.log(allUsers);
this.setState({
users: allUsers
})
});
}
handleChange = (event) => {
this.setState({
value: event.target.value,
});
console.log(this.state.value);
};
render(){
return(
<div>
<div>
<TextField
floatingLabelText="Search"
id="text-field-controlled"
value={this.state.value}
onChange={this.handleChange.bind(this)}
/>
<p>{this.state.value}</p>
</div>
{
this.state.users.map((user) => {
return <div key={user.id}>
<Users/>
</div>
})
}
</div>
)
}
let API = {
getAllUsers: function () {
console.log(this.props.params.login);
return new Promise(function (resolve, reject) {
axios.get('https://api.github.com/search/repositories', {
params: {
q: 'react',
per_page: 20
}
}).then(function (allUsers) {
resolve(allUsers.data.items);
console.log(allUsers.data.items)
})
})
}
}
Answer the question
In order to leave comments, you need to log in
pull the getAllUsers function and pass your parameter to it, but in general look towards redax
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question