zoukankan      html  css  js  c++  java
  • react萌新的读书笔记(一)


    书的地址

    链接: https://pan.baidu.com/s/1YC0qS2QFJO3yQ-kFt27lBQ 提取码: h2km
    

    子传父

    通过方法

    父组件

    export default class TheOne extends Component {
        constructor(props) {
            super(props);
        }
    
        setNum(e) {
            console.log(e);
        }
        render() {
            return (<>
                <MarginTop setNum={this.setNum}/>
            </>)
        }
    }
    

    子组件

    class MarginTop extends Component {
        constructor(props) {
            super(props);
        }
        onChangesOne =()=>{
            this.props.setNum('cccc')
        };
        render() {
            return <>
                <button onClick={this.onChangesOne}>Click</button>
            </>
        }
    }
    

    prop-types 父传子属性限制

        static propTypes = {
            age: PropType.number
        }
    

    defaultProps 设置默认值

    class First extends Component {
        constructor(props) {
            super(props);
        }
    
        static defaultProps = {
            num: 1,
            a: 2
        }
    
        render() {
            let {num,a}=this.props
            return <><h1>First--{num+a}</h1></>
        }
    }
    

    箭头函数的方式

     <MyButton disabled={false} test={'xxxx'}/>
    
    const MyButton = ({disabled, test}) => {
        return <button disabled={disabled}>{test}</button>
    }   
    

    模拟父传子的请求

    父
    export default class TestTwo extends Component {
    
        constructor(props) {
                super(props);
                this.state = {
                    arr:[]
                }
            }
        fetchDate(){
                return new Promise(res=>setTimeout(()=>{
                    res(['a','b','c'])
                },2000))
            }
            componentDidMount() {
                this.fetchDate().then(res=>{
                    this.setState({
                        arr:res
                    })
                })
    
            }
         render() {
                let {arr} = this.state
                return (<>
                    <MyButton disabled={false} test={arr}/>
                </>)
            }
    }
    
    const MyButton = ({test}) => {
        return (<>
            <ul>
                {test.map((val,i)=>{
                    return <li key={i}>{val}</li>
                })}
            </ul>
        </>)
    }
    

    插槽

     <TestTwo>
                    <TestTen/>
     </TestTwo>
    

    export default class TestTwo extends Component{
        constructor(props) {
            super(props);
        }
        componentDidMount() {
            console.log(this.props.children);
        }
    
        render(){
            return (<div>
                two
                {this.props.children} // 会把TestTen 放在这里
            </div>)
        }
    }
    

    如果想调整顺序

     <TestTwo>
                    <TestTen/>
                    <TestConfig />
    </TestTwo>
    
    ========
         <div>
                {this.props.children[0]}
                two
                {this.props.children[1]}
            </div>
    

    另一种方式的插槽

    父
     <TestTwo>
                    <TestTwo.First/>
     </TestTwo>
    子
    class First extends Component{
        render(){
            return <><h1>First</h1></>
        }
    }
    export default class TestTwo extends Component {
        constructor(props) {
            super(props);
        }
    
        render() {
            let {first, second} = this.state
            return (<>
              	Two
                {this.props.children}
            </>)
        }
    }
    
    TestTwo.First=First;
    

    Fragment

    对子列表分组,无需向DOM 添加节点

    <Fragment>
                {this.props.children[0]}
                two
                {this.props.children[1]}
     </Fragment>)
    
    短语法
    <>
    xxxx
    </>    
    

    key 是唯一可以传递给 Fragment 的属性

    上下文(Context)

    // context.js
    import React from 'react'
    
    export default React.createContext({})
    
    //theThree.js
    import React, {Component, createContext} from 'react'
    import TestTwo from "./TestTwo";
    import ThemeContext from './context'
    
    
    export default class TestThree extends Component {
        constructor(props) {
            super(props);
        }
    
        render() {
            return <>
                <h1>TestThree</h1>
                <ThemeContext.Provider value={{
                    foo:'bar',
                    baz:'blah'
                }}>
                    <TestTwo/>
                </ThemeContext.Provider>
            </>
        }
    }
    
    // TestTwo
    import React, {Component} from 'react'
    import ThemeContext from './context'
    
    export default class TestTwo extends Component {
        constructor(props) {
            super(props);
        }
        static contextType=ThemeContext;
        componentDidMount() {
            console.log(this.context);
        }
    
    
        render() {
            return <>
                <h1>TestTwo</h1>
            </>
        }
    }
    // 箭头函数版
    const Four=(props)=>{
        return (<ThemeContext.Consumer className="Consumer">
            {({foo,baz})=>{
                return (<h1>{foo}---{baz}</h1>)
            }}
        </ThemeContext.Consumer>)
    }
    // hooks 版
    const Four = (props) => {
        const count=useContext(ThemeContext)
        useEffect(() => {
            console.log(count);// 拿到值了
        });
        return (<>
            xxx
        </>)
    }
    

    如果跟我一样出现报错了

    createContext调用移动到单独的文件/模块可以解决我们的问题

    react-hooks

    useState

    双向绑定

    const Four = (props) => {
        const [age, setAge] = useState('xxx');
        return (<>
            <input type="text" value={age}
                   onChange={e => setAge(e.target.value)}
            />
            {age}
        </>)
    }
    

    把父的状态和修改都传给子修改

    const Four = (props) => {
        const age=useState(10)
        return (<>
            xxx
            <ThemeContext.Provider value={age}>
                {age[0]}
                <TestTwo />
            </ThemeContext.Provider>
        </>)
    }
    子
    export default class TestTwo extends Component {
        constructor(props) {
            super(props);
        }
        static contextType=ThemeContext;
        componentDidMount() {
        }
    
    
        render() {
            const [value,setValue]=this.context;
            return <>
                <h1>{value}</h1>
                <button onClick={()=>setValue(12)}>修改</button>
            </>
        }
    }
    如果是箭头函数
    const Fill=props=>{
        const [status,setStatus]=useContext(ThemeContext)
        return (<>
            {status}
            <button onClick={()=>setStatus(111)}>修改</button>
        </>)
    }
    

    useEffect

    清楚订阅

    useEffect(() => {
      const subscription = props.source.subscribe();
      return () => {
        // 清除订阅
        subscription.unsubscribe();
      };
    });
    
    useEffect(
      () => {
        console.log(1)
      },
      [props.source],
    );
    
    只有当props.source 改变后才会重新创建订阅
    

    决定自己的高度的是你的态度,而不是你的才能

    记得我们是终身初学者和学习者

    总有一天我也能成为大佬

  • 相关阅读:
    前端vue采用formData数据格式传递文件(Post请求)和非文件参数。后端怎样接收的问题(已解决)
    easyExcel 所需的maven 依赖
    基于客户数据的银行信用卡风险控制模型研究-金融风控模型标准评分卡
    chrome 插件知识点(转载)
    EasyNVR调用指定时间端录像播放接口出现黑屏的问题原因以及解决方法
    EasyNVR智能云终端推送视频流至EasyNVS管理平台失败且报错如何排查?
    EasyNVR分发rtsp不标准导致客户端检查报错是什么原因?
    EasyNVR前端在线修改录像存储路径后hls直播失效问题的处理
    EasyNVR实时阅览四分屏状态下最后一路流无法正常播放问题排查
    通过EasyNVR推流到抖音快手直播间无法正常推流是什么原因?
  • 原文地址:https://www.cnblogs.com/fangdongdemao/p/13529839.html
Copyright © 2011-2022 走看看