react基础<Ⅴ>

Day8

1.渲染App或者别的组件,一定要大写!!不能:function app(){}

2.结构:

{
2.1 四层一层一层往下传 * 两个案例
2.2 不需要一层一层往下传 * [(第一个案例+第二个案例react旧新两种写法)の重构 ]
3.1 解释第二个案例react旧的写法(使用babel+代码):标签中传函数
}

//vanilla js写法
//index.js
function F1(props){
  return <div>
      1,{props.n1}
      <F2 n2={props.n1}/>
  </div>
}
function F2(props){
  return <div>2,{props.n2}
      <F3 n3={props.n2}/>
  </div>
}
function F3(props){
  return <div>3,{props.n3}
      <F4 n4={props.n3}/>
  </div>
}
function F4(props){
  return <div>4,{props.n4}</div>
}


class App extends React.Component{
  constructor(){
      super();
      this.state = {
          n : 99
      }
  }
  render(){
      return (
          <div>
              <F1 n1={this.state.n}/>
          </div>
      )
  }
}

const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);
//react写法
import React from "react";
import ReactDOM from "react-dom";

import "./styles.css";


function Button(props){
  return  <button>{props.theme}</button>
}

class ThemedButton extends React.Component {
  render() {
    return <Button theme={this.props.theme} />;
  }
}

function Toolbar(props) {
  // Toolbar 组件接受一个额外的“theme”属性,然后传递给 ThemedButton 组件。
  // 如果应用中每一个单独的按钮都需要知道 theme 的值,这会是件很麻烦的事,
  // 因为必须将这个值层层传递所有组件。
  return (
    <div>
      <ThemedButton theme={props.theme} />
    </div>
  );
}
class App extends React.Component {
  render() {
    return <Toolbar theme="dark" />;
  }
}
const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);

问题:如果只有第四层函数需要n/theme值,也要前三层慢慢传下来,特别麻烦
设想:使用全局变量
问题:危险,有可能你拿到的值不是你想要的值
解决:使用ES6语法:局部的全局变量

//对于vanilla js
{
    let context = {}
    window.setContext = function(key, value){//把x暴露出来
        context[key] = value
    }
    window.f1 = function F1(){
        console.log(1)
        </div>
    }
    function f2(){
        console.log(2)
    }
    function f3(){
        console.log(3)
    }
    function f4(){
        console.log(4,context['n'])
        //让需要n值的函数处于一个局部作用域里,n是这个局部作用域的全局变量,外界访问不了
    }
    //f1,f2,f3,f4四个函数其实就是闭包,也就是在局部作用域中的函数
    //优点:一个是前面提到的可以读取函数内部的变量,另一个就是让这些变量的值始终保持在内存中,不     //会在f1调用后被自动清除。
    //缺点:由于闭包会使得函数中的变量都被保存在内存中,内存消耗很大
}

{
    window.setContext('n', 100)
    window.f1()//把F1()暴露出来
    consloe.log("done")
}

注意:在ES5想造一个局部,需要使用立即执行函数

function (){

}()
//长这样婶的
//对于react
function F1() {
  return (
    <div>
      1
      <F2 />
    </div>
  );
}
function F2() {
  return (
    <div>
      2
      <F3 />
    </div>
  );
}
function F3(props) {
  return (
    <div>
      3,
      <ThemeContext.Consumer>
        {(n)=><F4 n4={n} />}
      </ThemeContext.Consumer>
    </div>
  );
}
function F4(props) {
  return <div>4,{props.n4}</div>;
}

const ThemeContext = React.createContext();

class App extends React.Component {
  render() {
    return (
      <div>
        <ThemeContext.Provider value="99">
          <F1 />
        </ThemeContext.Provider>
      </div>
    );
  }
}

const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);
//react最新语法,这个更便于理解
function Button(props) {
  return <button>{props.theme}</button>;
}

class ThemedButton extends React.Component {
  // 指定 contextType 读取当前的 theme context。
  // React 会往上找到最近的 theme Provider,然后使用它的值。
  // 在这个例子中,当前的 theme 值为 “dark”。
  static contextType = ThemeContext;
  render() {
    return <Button theme={this.context} />;
  }
}

// 中间的组件再也不必指明往下传递 theme 了。
function Toolbar() {
  return (
    <div>
      <ThemedButton />
    </div>
  );
}

const ThemeContext = React.createContext();
// Context 可以让我们无须明确地传遍每一个组件,就能将值深入传递进组件树。

class App extends React.Component {
  render() {
    // 使用一个 Provider 来将当前的 theme 传递给以下的组件树。
    // 无论多深,任何组件都能读取这个值。
    // 在这个例子中,我们将 “dark” 作为当前的值传递下去。
    return (
      <ThemeContext.Provider value="dark">
        <Toolbar />
      </ThemeContext.Provider>
    );
  }
}

const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);

3.解释 {(n)=><F4 n4={n}/>}//标签里传函数

首先了解JSX中{F1}来传函数(链接:https://babeljs.io/

function F1(){
    return <div>123456</div>
}

<div className="login" title="login_title">
   F1
  {F1}//在标签里传函数
  <F1 /> //<F1 />这样写就会将<div>123456</div>渲染到这里
</div>

//<F1 />这样写就会将<div>123456</div>渲染到这里
"use strict";

function F1() {
  return React.createElement("div", null, "123456");
}

React.createElement("div", {className: "login",title: "login_title"}, 
"F1", 
F1, 
React.createElement(F1, null));

自己写一个组件,来解释(这个先放一放,还没懂)

function Consumer(props){
  let x = 100
  props.children(x)
  return (
    <div>{props.children}</div>
  )
}

function App (){
      return (
        <Consumer>
          {(n)=>console.log('我得到的信息是', n)}
        </Consumer>
      );
    
}

const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);
    
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • useState 1.基本使用 等价于 2. 复杂的state 3.使用状态 4. 注意事项 1). 如果stat...
    sweetBoy_9126阅读 3,259评论 0 6
  • 目前,react组件有三种写法,分别是es5的createClass写法,es6的class写法,以及statel...
    ZoomFunc阅读 1,911评论 0 1
  • 以下内容是我在学习和研究React时,对React的特性、重点和注意事项的提取、精练和总结,可以做为React特性...
    科研者阅读 8,409评论 2 21
  • HTML模版 之后出现的React代码嵌套入模版中。 1. Hello world 这段代码将一个一级标题插入到指...
    ryanho84阅读 6,447评论 0 9
  • react 基本概念解析 react 的组件声明周期 react 高阶组件,context, redux 等高级...
    南航阅读 1,139评论 0 1

友情链接更多精彩内容