React(3)

1.案例选项卡

import React, { Component } from 'react'

export default class App extends Component {

    state={
        tabList:[
            {
                id:1,
                text:"电影"
            },
            {
                id:2,
                text:"影院"
            },
            {
                id:3,
                text:"我的"
            }
        ]
    }

    render() {
        return (
            
    { this.state.tabList.map(item=>
  • {item.text}
  • ) }
) } }

React(3)_第1张图片

进行样式修改:js引入css

import './css/02_tab.css'
*{
    margin: 0;
    padding: 0;
}

ul{
    list-style: none;
    display: flex;
    position: fixed;
    bottom: 0px;
    left: 0px;
    height: 50px;
    line-height: 50px;
    width: 100%;
}


ul li{
    flex:1;
    text-align:center
}

 React(3)_第2张图片

 

 

鼠标点击高亮显示

 

.active{
    color: red;
}
import React, { Component } from 'react'
import './css/02_tab.css'

export default class App extends Component {

    state={
        tabList:[
            {
                id:1,
                text:"电影"
            },
            {
                id:2,
                text:"影院"
            },
            {
                id:3,
                text:"我的"
            }
        ],
        currentKey:0
    }

    render() {
        return (
            
    { this.state.tabList.map((item,index)=>
  • this.handlerClick(index) }>{item.text}
  • ) }
) } handlerClick(index){ this.setState({ currentKey:index }) } }

 

React(3)_第3张图片

三个组件显示

新建三个组件

import React, { Component } from 'react'

export default class Film extends Component {
    render() {
        return (
            
电影组件
) } }

import React, { Component } from 'react'
import './css/02_tab.css'

import Film from './tabComponent/film'
import My from './tabComponent/my'
import Cinema from './tabComponent/cinema'



export default class App extends Component {

    state={
        tabList:[
            {
                id:1,
                text:"电影"
            },
            {
                id:2,
                text:"影院"
            },
            {
                id:3,
                text:"我的"
            }
        ],
        currentKey:0
    }

    render() {
        return (
            
{this.showTable()}
    { this.state.tabList.map((item,index)=>
  • this.handlerClick(index) }>{item.text}
  • ) }
) } handlerClick(index){ this.setState({ currentKey:index }) } showTable(){ switch(this.state.currentKey){ case 0: return case 1: return case 2: return } } }

点击可以切换

 

React(3)_第4张图片

2.请求数据

react中使用axios第三方的库 ,专门用来请求数据

 先安装

npm i axios

 

import React, { Component } from 'react'
import axios from 'axios'

export default class Cinema extends Component {


    constructor() {
        super();
        //react中使用axios第三方的库  专门用来请求数据
        // axios.get("请求地址").then(res=>{}).catch(err=>{console.log(err);})
        axios({
            url:"https://m.maizuo.com/gateway?cityId=110100&ticketFlag=1&k=7406159",
            method:'get',
            headers:{
                'X-Client-Info':'{"a":"3000","ch":"1002","v":"5.0.4","e":"16395416565231270166529","bc":"110100"}',
                'X-Host':'mall.film-ticket.cinema.list'
            }
        }).then(res=>{
            console.log(res)}
         ).catch(err=>console.log(err))


    }

    render() {
        return (
            
影院组件
) } }

React(3)_第5张图片

res.data

*{
    margin: 0;
    padding: 0;
}

ul{
    list-style: none;
    display: flex;
    position: fixed;
    bottom: 0px;
    left: 0px;
    height: 50px;
    line-height: 50px;
    width: 100%;
    background-color: white;
}


ul li{
    flex:1;
    text-align:center
}

.active{
    color: red;
}

dl{
    height: 50px;
    border-bottom: 1px solid gray;
}
dl dt{
    font-size: 20px;
}
dl dd{
    font-size: 12px;
    color: gray;
}

 

import React, { Component } from 'react'
import axios from 'axios'

export default class Cinema extends Component {


    constructor() {
        super();
        this.state = {
            cinemaList: []
        }
        //react中使用axios第三方的库  专门用来请求数据
        // axios.get("请求地址").then(res=>{}).catch(err=>{console.log(err);})
        axios({
            url: "https://m.maizuo.com/gateway?cityId=110100&ticketFlag=1&k=7406159",
            method: 'get',
            headers: {
                'X-Client-Info': '{"a":"3000","ch":"1002","v":"5.0.4","e":"16395416565231270166529","bc":"110100"}',
                'X-Host': 'mall.film-ticket.cinema.list'
            }
        }).then(res => {
            console.log(res.data)
            this.setState({
                cinemaList: res.data.data.cinemas
            })
        }).catch(err => console.log(err))


    }

    render() {
        return (
            
{this.state.cinemaList.map((item) =>
{item.name}
{item.address}
)}
) } }

 React(3)_第6张图片

3.模糊搜索

利用input  属性onInput可以实时监测输入框变化

 先尝试监控输入框改变并打印改变的内容

import React, { Component } from 'react'
import axios from 'axios'

export default class Cinema extends Component {


    constructor() {
        super();
        this.state = {
            cinemaList: []
        }
        //react中使用axios第三方的库  专门用来请求数据
        // axios.get("请求地址").then(res=>{}).catch(err=>{console.log(err);})
        axios({
            url: "https://m.maizuo.com/gateway?cityId=110100&ticketFlag=1&k=7406159",
            method: 'get',
            headers: {
                'X-Client-Info': '{"a":"3000","ch":"1002","v":"5.0.4","e":"16395416565231270166529","bc":"110100"}',
                'X-Host': 'mall.film-ticket.cinema.list'
            }
        }).then(res => {
            console.log(res.data)
            this.setState({
                cinemaList: res.data.data.cinemas
            })
        }).catch(err => console.log(err))


    }

    render() {
        return (
            
实时搜索
{this.state.cinemaList.map((item) =>
{item.name}
{item.address}
)}
) } handleInput(event){ console.log("input",event.target.value); } }

 

React(3)_第7张图片

实时模糊搜索

数组的filter方法不会影响原数组

import React, { Component } from 'react'
import axios from 'axios'

export default class Cinema extends Component {


    constructor() {
        super();
        this.state = {
            cinemaList: [],
            backcinemaList: []
        }
        //react中使用axios第三方的库  专门用来请求数据
        // axios.get("请求地址").then(res=>{}).catch(err=>{console.log(err);})
        axios({
            url: "https://m.maizuo.com/gateway?cityId=110100&ticketFlag=1&k=7406159",
            method: 'get',
            headers: {
                'X-Client-Info': '{"a":"3000","ch":"1002","v":"5.0.4","e":"16395416565231270166529","bc":"110100"}',
                'X-Host': 'mall.film-ticket.cinema.list'
            }
        }).then(res => {
            console.log(res.data)
            this.setState({
                cinemaList: res.data.data.cinemas,
                backcinemaList: res.data.data.cinemas
            })
        }).catch(err => console.log(err))


    }

    render() {
        return (
            
实时搜索
{this.state.cinemaList.map((item) =>
{item.name}
{item.address}
)}
) } handleInput = (event) => { console.log("input", event.target.value); // 数组的filter方法不会影响原数组 var newList = this.state.backcinemaList.filter(item => item.name.toUpperCase().includes(event.target.value.toUpperCase()) || item.address.toUpperCase().includes(event.target.value.toUpperCase())) this.setState({ cinemaList:newList }) } }

React(3)_第8张图片

 

4.setState说明

setState在同步代码使用时是异步的,在异步代码使用时同步的

 也就说setState更新状态,可能react背后不是立即就更新state的  所以如果你再setState方法后紧随其后获取当前state中的值,可能会发现获取的值还是之前没更新的,更新并没有生效

方法:

如果是在同步执行代码中,可以使用setState的回调函数,回调函数中一定是已经更新完状态的

    this.setState({
                cinemaList: res.data.data.cinemas,
                backcinemaList: res.data.data.cinemas
    },()=>{
         //回调函数中执行你的需求       
    })

如果是在异步中就无所谓了

5.平滑滚动better-scroll

引入better-scroll库

npm i better-scroll

作用就是使一个显示很长的数据可以在一个固定很短的区域中显示滑动   要求必须一个很短的区域  包裹一个很长的区域

就拿上文中的cinemaList来做 

修改:

import React, { Component } from 'react'
import axios from 'axios'
import BetterScroll from 'better-scroll'

export default class Cinema extends Component {


    constructor() {
        super();
        this.state = {
            cinemaList: [],
            backcinemaList: []
        }
        //react中使用axios第三方的库  专门用来请求数据
        // axios.get("请求地址").then(res=>{}).catch(err=>{console.log(err);})
        axios({
            url: "https://m.maizuo.com/gateway?cityId=110100&ticketFlag=1&k=7406159",
            method: 'get',
            headers: {
                'X-Client-Info': '{"a":"3000","ch":"1002","v":"5.0.4","e":"16395416565231270166529","bc":"110100"}',
                'X-Host': 'mall.film-ticket.cinema.list'
            }
        }).then(res => {
            console.log(res.data)
            this.setState({
                cinemaList: res.data.data.cinemas,
                backcinemaList: res.data.data.cinemas
            })

            new BetterScroll(".wrapper")
        }).catch(err => console.log(err))


    }

    render() {
        return (
            
实时搜索
{this.state.cinemaList.map((item) =>
{item.name}
{item.address}
)}
) } handleInput = (event) => { console.log("input", event.target.value); // 数组的filter方法不会影响原数组 var newList = this.state.backcinemaList.filter(item => item.name.toUpperCase().includes(event.target.value.toUpperCase()) || item.address.toUpperCase().includes(event.target.value.toUpperCase())) this.setState({ cinemaList: newList }) } }

此时滚动很丝滑  还带点惯性哈哈哈哈哈

你可能感兴趣的:(react,react.js,javascript,前端)