spring mvc
@PostMapping("/categories")
public List<Category> save(@RequestBody Category category) {
categoryService.save(piece);
return queryList();
}
@GetMapping("/categories")
public List<Category> queryList() {
return categoryService.queryList();
}
@DeleteMapping("/categories/{id}")
public List<Category> delete(@PathVariable int id) {
categoryService.delete(id);
return queryList();
}
redux-api
export default reduxApi({
categories: {
url: '/categories',
transformer: transformers.array,
crud: true
},
categoryDetail: {
url: '/categories/:id',
transformer: transformers.array,
crud: true,
reducerName: 'categories'
}
}).use('fetch', adapterFetch);
这样增删改查之后就能使用同一个对象"categories"
import rest from 'rest'
class CategoryList extends React.Component {
constructor(porps) {
super(props)
}
componentDidMount() {
this.props.dispatch(rest.categories.get())
}
render() {
const {categories} = this.props
if (categories.isLoading) {
return <div>loading...</div>
}
return (
<div id='categoryList'>
{categories.data.map((item)=>
<div key={item.id}>
<span>item.name</span>
<span onClick={()=>{
this.props.dispatch(rest.categories.delete({id:item.id}))
}}>X</span>
</div>
)}
</div>
)
}
}
export default connect((state)=> {
const {categories} = state
return {categories}
}, CategoryList)