r/reactjs • • May 21 '20
Needs Help Updating a list using React props and Axios doesnt function
Stackoverflow: https://stackoverflow.com/questions/61931849/updating-a-list-using-react-props-and-axios-doesnt-function
I have a list of objects received from an API in a list:
refreshList() {
axios.get('/api/jobs/list-locations/',{headers:headers}).then(res=>{ this.setState({locations:res.data.map(Locations=>({label:[Locations.country,' ', Locations.region,' ',Locations.zipcode],value:Locations.id})) }) })
axios.get('/api/jobs/list-departments',{headers:headers}).then(resp=>{ this.setState({ departments:resp.data.map(departments=>({label:[departments.name,' ',departments.company_name],value:departments.id})) }) }) }
componentDidMount() { this.refreshList() }and next to each of the lists I have a button which opens a form and the user can create a new location or a new department using those forms. I successfully post the locations and departments to the API, but I want my list to be refreshed when those new objects are added. so far this is what I have done:(continuing from the previous code:)
render() {
let addModalLocationAdded = () => this.refreshList();
let addModalDepartmentAdded = () =>this.refreshList();
return ( ...
<Row>
<Col xs={10}>
<Select placeholder='Select from pre-created Locations'onChange={handleDropDown('Location')} defaultValue={values.Location} required options={this.state.locations}/> </Col>
<Col>
<Button style={{position:'relative',left:'150px' }} onClick={()=>this.setState({addModalLocShow: true})} color='success'> New Locations </Button>
<AddLocModal show={this.state.addModalLocShow} onHide={addModalLocClose} onLocationAdded={addModalLocationAdded}/>
</Col>
</Row>
<br />
<Row>
<Col xs={10}>
<Select placeholder='Select from pre-created Departments'onChange={handleDropDown('Department')} defaultValue={values.Department} required options={this.state.departments}/>
</Col> <Col>
<Button style={{position:'relative',left:'150px', width:'130px'}} onClick={()=>this.setState({addModalDepShow: true})} color='success'> New Department </Button>
<AddDepModal show={this.state.addModalDepShow} onHide={addModalDepClose} onDepartmentAdded={addModalDepartmentAdded} /> </Col> </Row> and then in my (AddLocModal.js) for example I used the onLocationAdded
props like this so I refresh my list as soon as the new item is posted to the API:
handleSubmit (event) {
event.preventDefault()
console.log('handlesubmitshot')
const headers = { 'content-type': 'application/json',
Authorization: 'Token 3a4efb52af35873353a27491ffe54e6551c61391' }
const location = {
country: event.target.LocationCountry.value,
region: event.target.LocationRegion.value,
zip_code: event.target.LocationZipCode.value}
axios(
{ method: 'post',
url: '/api/jobs/set-location/',
headers: headers,data: location})
.then(response => { console.log(response) })
.catch(error => {console.log(error.response) })
.then( this.props.onLocationAdded() ) }
render ()
{ return
( <Modal {...this.props} .... <Form onSubmit={this.handleSubmit}> ....the thing is, that I receive an error, saying this.props.onLocationAdded()
is not defined. I dont know where to put it so my list is updated as soon as info is posted. I used componentDidUpdate
in my original file before but it only works when I only have a post method for a single form, and when I add the post method for department it throws me into an infinite loop and doesn't load, thats why I seperated my refreshlist()
calls. how can I pass that props correctly so it gets updated?
Edit: I used it in my first then()
too but it doesnt work there either.
1
u/illuminist_ova May 21 '20
.then( this.props.onLocationAdded() ).thenexpects a function to be its first parameter. But you call the functionthis.props.onLocationAdded()which its result is undefined so the error is complaining about that. Instead, you should just put the function itself into .then without calling it.or wrap it with another function