react新版和旧版的区别

低版本:15.6

npm install create-react-app -g 全局环境的安装

create-react-app name

cd name

npm start  运行

删除package.lock.json文件

npm install react@15.6  react-dom@15.6 jquery --save-dev


传值:

1: params  =》 <Link to="/about/2" activeStyle={style}>关于</Link>  

绑定属性=><Route path="/about/:id" component={About}></Route>

取值=》this.props.params.id

2:query =》 <Link to={{pathname:"/other",query:{id:3}}}>其他</Link>

取值 =》this.props.location.query.id

重定向  <IndexRedirect to=""/> 
      <IndexRoute component={Index}/>    <Rerirect from=""to=""


跳转 this.props.history.push('/home')


视图容器:{this.props.children}


嵌套路由动态监听==》

componentWillReceiveProps(a){
        console.log(a)
        var _this=this;
        $.ajax({
            type:"post",
            url:"http://datainfo.duapp.com/shopdata/getGoods.php",
            dataType:"jsonp",
            // data:({goodsID:_this.props.params.id}),
            data:({goodsID:a.location.query.id}),
            success:function(data){
                _this.setState({str:data[0].detail})
            }
        })
}


高版本:16.4


npm install create-react-app -g 全局环境的安装

create-react-app name

cd name

npm install react-router jquery --save-dev

npm install react-router-dom --save-dev

npm start  运行

导入模块: 
import {BrowserRouter as Router,Route,Link,Redirect} from 'react-router-dom'

路由  

 render() {
    return (
      <div>
          <h1>new版本</h1>
          
      <Router>
        <div>
          <Link to="/home">首页</Link>
          <Link to="/about">关于</Link>
          <hr/>
          <Switch>
            
            <Route path="/home" component={Home}></Route>
            <Route path="/about" component={About}></Route>
            <Redirect to="/home"/>
          </Switch>
        </div>
      </Router>
      </div>
    );
  }
嵌套路由


 render(){
        return(
            <div>
                <h1>首页Home</h1>
                <Router>
                    <div>
                        <ul>
                        {                   
                            this.state.arr.map(function(item,i){
                                return(
                                    <li key={i}><Link to={"/detial/"+item.goodsID}>        
            {item.goodsName}</Link></li>
                                    // <li key={i}><Link to={{pathname:"/detial",query:        
        {id:item.goodsID}}}>{item.goodsName}</Link></li>
                                )
                            })
                        }
                    </ul>
                    <hr />
                    <Route path="/detial/:id" component={Detial}></Route>
                
                    </div>
                
                </Router>   
            </div>
        )

    }

Link写在Router内部形成路由结构(必须写在)


传值 params
取值 this.props.match.params.xxx

传值 query
取值 this.props.location.query.xxx


重定向 :<Redirect to="/about"/>

跳转:hashHistory.push("/other")

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

沫熙瑾年

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

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

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

打赏作者

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

抵扣说明:

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

余额充值