react项目踩过的坑

1.map遍历提示需要加key

2.a标签废弃 href="javascript:;",href值必需为url

3.改变数组项的值,页面不渲染,正确的修改state值应该为this.setState({
                    data:value,
         })

this.state = {
tabBar: [
                {
                    name: '大会概况',
                    href: 'introduction',
                    isShow: 'block',
                },
                {
                    name: '会议嘉宾',
                    href: 'guest',
                    isShow: 'block',
                },
                {
                    name: '会议议程',
                    href: 'agenda',
                    isShow: 'block',
                },
                {
                    name: '展位预定',
                    href: 'positionReserve',
                    isShow: 'none',
                },
                {
                    name: '赞助方案',
                    href: 'sponsor',
                    isShow: 'none',
                },
                {
                    name: '酒店预定',
                    href: 'hotelReserve',
                    isShow: 'none',
                },
                {
                    name: '立刻报名',
                    href: 'signIn',
                    isShow: 'none',
                },
            ],
}
methodsFun() {
        const tabBarList = this.state.tabBar.map(item => {
            item.isShow = item.href === tabInf.href ? 'block' : 'none'
            return item
        })
        // 错误
        this.state.tabBar = [...tabBarList ]; 
        // 正确
        this.setState({
                    tabBar: [...tabBarList],
         })
}

4.react解析带有html标签的字符串

const content = {__html:'aaaa<br /> ddddd'};
<div dangerouslySetInnerHTML={content}>

5.react button绑定click事件传参,未点击触发

错误代码如下:<button className="btn btn-default" onClick={(this.myclickHandler('pig','dog')}>按钮</button>

import React from 'react'
 
export default class BindEvent extends React.Component{
    constructor(){
        super()
        this.state={
            msg:'dog'
        }
    }
    render(){
        return <div>
            <button className="btn btn-default" onClick={this.myclickHandler('pig','dog')}>按钮</button>
            <hr/>
            <h3>{this.state.msg}</h3>
        </div>
    }
    myclickHandler=(arg1,arg2)=>{
       this.setState({
           msg:'yellow dog'+arg1+arg2
       })
    }
}

正确代码如下:<button className="btn btn-default" onClick={()=>{this.myclickHandler('pig','dog')}}>按钮</button>

import React from 'react'
 
export default class BindEvent extends React.Component{
    constructor(){
        super()
        this.state={
            msg:'dog'
        }
    }
    render(){
        return <div>
            <button className="btn btn-default" onClick={()=>{this.myclickHandler('pig','dog')}}>按钮</button>
            <hr/>
            <h3>{this.state.msg}</h3>
        </div>
    }
    myclickHandler=(arg1,arg2)=>{
       this.setState({
           msg:'yellow dog'+arg1+arg2
       })
    }
}

6.userState遇到的坑

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

麦兜_冰夕

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值