房源发布模块
目标
- 如何解决JS 文本输入框防抖(用户输入过快导致请求服务器的压力过大)
- 能够完成搜索模块
- 能够获取发布房源的相关信息
- 能够知道图片上传的流程
- 能够完成图片上传功能
- 能够完成房源发布功能
前期准备工作
功能
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-d0yllDAe-1575112741215)(images/发布房源.png)]](https://i-blog.csdnimg.cn/blog_migrate/085aebce62001c4c405a8cf85f27c5c4.png)
模板改动说明
- 修改首页(Index)去出租链接为: /rent/add
- 修改公共组件NoHouse的children属性校验为: node(任何可以渲染的内容)
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-wTlhzzyO-1575112741216)(images/noHouse.jpg)]](https://i-blog.csdnimg.cn/blog_migrate/95bcfaf6337d0e1f78603f43c4e22809.png)
- 修改公共组件HousePackage,添加onSelect的默认值
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-MuCgqTDd-1575112741220)(images/onSelect.png)]](https://i-blog.csdnimg.cn/blog_migrate/67ed9462cfa571e080b7bca2de42b122.png)
- 添加utils/city.js,封装当前定位城市 localStorage的操作
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-oUpPCXcv-1575112741223)(images/city.png)]](https://i-blog.csdnimg.cn/blog_migrate/4122faf1c322ada0490e549e576cb04d.png)
- 创建了三个页面组件:Rent(已发布房源列表)、Rent/Add(发布房源)、Rent/Search(关键词搜索校区信息)
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-GZfe1Qn6-1575112741227)(images/rent.png)]](https://i-blog.csdnimg.cn/blog_migrate/11ed1a24f68c66340a7047ee92ba205e.png)
import React, { Component } from 'react'
import { Link } from 'react-router-dom'
import { API, BASE_URL } from '../../utils'
import NavHeader from '../../components/NavHeader'
import HouseItem from '../../components/HouseItem'
import NoHouse from '../../components/NoHouse'
import styles from './index.module.css'
export default class Rent extends Component {
state = {
// 出租房屋列表
list: []
}
// 获取已发布房源的列表数据
async getHouseList() {
const res = await API.get('/user/houses')
const { status, body } = res.data
if (status === 200) {
this.setState({
list: body
})
} else {
const { history, location } = this.props
history.replace('/login', {
from: location
})
}
}
componentDidMount() {
this.getHouseList()
}
renderHouseItem() {
const { list } = this.state
const { history } = this.props
return list.map(item => {
return (
<HouseItem
key={item.houseCode}
onClick={() => history.push(`/detail/${item.houseCode}`)}
src={BASE_URL + item.houseImg}
title={item.title}
desc={item.desc}
tags={item.tags}
price={item.price}
/>
)
})
}
renderRentList() {
const { list } = this.state
const hasHouses = list.length > 0
if (!hasHouses) {
return (
<NoHouse>
您还没有房源,
<Link to="/rent/add" className={styles.link}>
去发布房源
</Link>
吧~
</NoHouse>
)
}
return <div className={styles.houses}>{this.renderHouseItem()}</div>
}
render() {
const { history } = this.props
return (
<div className={styles.root}>
<NavHeader onLeftClick={() => history.go(-1)}>房屋管理</NavHeader>
{this.renderRentList()}
</div>
)
}
}
三个路由规则配置
- 在App.js 中导入Rent已发布房源列表页面
- 在App.js 中导入AuthRoute组件
- 使用AuthRoute组件,配置路由规则
- 使用同样方式,配置Rent/Add 房源发布页面,Rent/Search 关键词搜索小区信息页面
{/* 配置登录后,才能访问的页面 */}
<AuthRoute exact path="/rent" component={Rent} />
<AuthRoute path="/rent/add" component={RentAdd} />
<AuthRoute path="/rent/search" component={RentSearch} />
搜索模块(★★★)
关键词搜索小区信息
- 获取SearchBar 搜索栏组件的值
- 在搜索栏的change事件中,判断当前值是否为空
- 如果为空,直接return,不做任何处理
- 如果不为空,就根据当前输入的值以及当前城市id,获取该关键词对应的小区信息
- **问题:**搜索栏中每输入一个值,就发一次请求,这样对服务器压力比较大,用户体验不好
- **解决方式:**使用定时器来进行延迟执行(关键词:JS文本框输入 防抖)
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-OtWoClFE-1575112741229)(images/输入框防抖.png)]](https://i-blog.csdnimg.cn/blog_migrate/d3195bdd1b574f6c6a3a3ddc92aec72b.png)