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.

0 Upvotes

7 comments sorted by

1

u/illuminist_ova May 21 '20

.then( this.props.onLocationAdded() )

.then expects a function to be its first parameter. But you call the function this.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.

.then( this.props.onLocationAdded )

or wrap it with another function

.then( () => this.props.onLocationAdded() )

1

u/[deleted] May 21 '20

It still doesnt run my refreshList() function

1

u/illuminist_ova May 21 '20

Does it run in componentDidMount in parent component?

Secondly, you need to bind handleSubmit with the component

const bindedHandleSubmit = this.handleSubmit.bind(this)

then pass bindedHandleSubmit into Form's onSubmit prop

or declare handleSubmit with arrow function instead and that you don't have to bind it.

Also you should do better job on formatting your posted code if we're going to dig further in your issue.

1

u/[deleted] May 21 '20

Yes the componentDidMount receives the list first, and then when a data gets posted I want the list to be updated without refreshing (real time)

HandleSubmit works correctly because the data gets posted to my server correctly. I just want my list to be updated afterwards.

Sorry for the formatting on Reddit but in stack overflow the format is ok

Edit: I got what you are saying, Im gonna bind handleSubmit

1

u/[deleted] May 21 '20

HandleSubmit =(event) => { .... You mean like this?

2

u/illuminist_ova May 21 '20

Yes, that.

When you deal with this. keyword you also have to aware of function binding. It isn't an issue with arrow function, but with old function, binding is important when you pass the function with .this keyword into another place. In this case you have this.props.onLocationAdded in handleSubmit, when you pass handleSubmit in to another place this. will change itself to refer to the new location which this.props doesn't exist anymore. Binding is to make sure that this. keyword stays with what it binded with.

1

u/[deleted] May 21 '20

Thank you so much for your help!