javascript - 使用 React 道具和 Axios 更新列表不起作用
问题描述
我有一个从列表中的 API 接收到的对象列表:
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()
}
在每个列表旁边我都有一个按钮,可以打开一个表单,用户可以使用这些表单创建一个新位置或一个新部门。我成功地将位置和部门发布到 API,但我希望在添加这些新对象时刷新我的列表。到目前为止,这就是我所做的:(继续前面的代码:)
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>
然后在我的(AddLocModal.js)中,例如,我使用了onLocationAdded
这样的道具,所以一旦新项目发布到 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}>
....
问题是,我收到一个错误,说this.props.onLocationAdded()
没有定义。我不知道把它放在哪里,所以我的列表会在信息发布后立即更新。我componentDidUpdate
以前在我的原始文件中使用过,但它只在我只有一个表单的 post 方法时才有效,当我为部门添加 post 方法时,它会让我陷入无限循环并且不加载,这就是我分开我的原因refreshlist()
来电。如何正确传递该道具以便更新?
编辑:我也第一次使用它,then()
但在那里也不起作用
解决方案
推荐阅读
- rust - Rust:重构后结构的可见性现在公开;无法将 main 添加到 pub(在 crate::main 中)
- performance - Spark 3 中的自适应查询执行
- r - 使用 for 循环添加具有多行的 ggplot2 图例
- ios - 如何设置子视图的属性?
- git - 当我的终端上的 git master 分支附加到我的主文件夹并且 GitHub 上的存储库被删除时,如何删除它?
- php - 如何将数据插入两个表?
- sql-server - 为什么分离后无法从备份中恢复数据库?
- keras - 具有非常不同的输入和输出维度的卷积网络
- r - c(... %*% ...) 和 sum(... * ...) 之间的区别
- git - 在分支中创建的 Git 文件在 master 中也可见