react props常用场景

1.  通过props传递一个组件

2. this.props.children

3. props传递方法

4. props传递数据

react/first-react/src/App.js
 
import Home from './views/home';
import Test from './views/Test';
import Props from './views/Props';
 
function App() {
  return (
    <div>
        <h3>这是第一个app</h3>
        {/*
            <Home/>
            <Test/>
            <PropsBak/>
        */}
        <Props/>
    </div>
  );
}
export default App;
————————————————
版权声明:本文为CSDN博主「c三只布朗熊」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/weixin_46347206/article/details/118659637
react/first-react/src/views/Props/index.jsx
 
import React, { Component } from 'react';
 
import One from './one.jsx';
import Two from './two.jsx';
import Three from './three.jsx';
import Four from './four.jsx';
import Five from './five.jsx';
import Five2 from './five2.jsx';
 
class View extends Component {
    render(){
        const obj = {
            name:'元芳',
            age:32
        }
        const obj2 = {
            cls:'web1908',
            zy:'web'
        }
        return(
            <div className="props_main">
                <h3>props的使用场景有哪些</h3>
                <One title="全国名师推荐"/>
                <One title="热门名师"/>
                <One title="名师热度榜"/>
                <Two data={obj} {...obj2}/>
                <Three title="全国名师推荐">
                    <a href="true"><<更多</a>
                </Three>
                <Three/>
                <Four left={<Five/>}/>
                <Four left={<Five2/>}/>
            </div>
        )
    }
}
export default View;
————————————————
版权声明:本文为CSDN博主「c三只布朗熊」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/weixin_46347206/article/details/118659637

 

react/first-react/src/views/Props/one.jsx
 
import React, { Component } from 'react';
 
class View extends Component {
 
    render(){
        console.log(this.props)
        return(
            <div>
                <h4>第一种使用场景</h4>
                {this.props.title}
            </div>
        )
    }
}
export default View;

————————————————
版权声明:本文为CSDN博主「c三只布朗熊」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/weixin_46347206/article/details/118659637
react/first-react/src/views/Props/two.jsx
import React, { Component } from 'react';
 
class View extends Component {
 
    render(){
        console.log(this.props)
        const {name ,age } = this.props.data;
        const {data,cls,zy} = this.props;
        return(
            <div>
                <h4>第一种使用场景</h4>
                名字是:{this.props.data.name}<br/>
                {name}<br/>
                {data.age}<br/>
                {cls}<br/>
                {zy}<br/>
            </div>
        )
    }
}
export default View;

react/first-react/src/views/Props/three.jsx
import React, { Component } from 'react';
 
class View extends Component {
 
    render(){
        return(
            <div>
                <h4>第二种使用场景</h4>
                {this.props.children}
            </div>
        )
    }
}
export default View;

react/first-react/src/views/Props/four.jsx
import React, { Component } from 'react';
 
class View extends Component {
 
    render(){
        console.log(this.props)
        return(
            <div>
                <h4>第三种使用场景</h4>
                {this.props.left}
            </div>
        )
    }
}
export default View;

react/first-react/src/views/Props/five.jsx
import React, { Component } from 'react';
 
class View extends Component {
 
    render(){
        return(
            <div className="props_five">
                <h4>第一个左侧</h4>
            </div>
        )
    }
}
export default View;
react/first-react/src/views/Props/five2.jsx
import React, { Component } from 'react';
 
class View extends Component {
 
    render(){
        return(
            <div className="props_five">
                <h4>第二个左侧</h4>
            </div>
        )
    }
}
export default View;

react/first-react/src/styles/index.scss
@charset "UTF-8";
// 默认样式
@import "base.scss";
// props
@import "props.scss";
// Test
// @import "test.scss";
//Header
// @import "header.scss";

react/first-react/src/styles/props.scss
@charset "UTF-8";
.props_main{
    width:500px;
    margin:0 auto;
    h3 {
        font-size:16px;
        line-height: 24px;
    }
}
.props_five {
    width:100px;
    border:1px solid red;
    height:100px;
}

 

 

 

 

 

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值