美文网首页
1-5 函数式组件

1-5 函数式组件

作者: bestCindy | 来源:发表于2020-07-09 23:12 被阅读0次

demo1 - 函数式组件

//函数式组件
function Childcom() {
  let title = <h2>我是副标题</h2>

  let weather = "下雨";
  let isGo = weather === "下雨" ? "不出门" : "出门";

  return (
      <div>
        <h1>函数式组件 hello world!</h1>
        {title}

        <div>
          是否出门?
          <span>{isGo}</span>
        </div>
      </div>
  )
}
ReactDOM.render(<Childcom />, document.querySelector("#root"));

demo2 - 类组件

//类组件
class HelloWorld extends React.Component {
    render() {
        return (
          <div>
              类组件 Hello World!
          </div>
        )
    }
}
ReactDOM.render(<HelloWorld />, document.querySelector("#root"));

demo3 - 函数式组件传参

function Childcom(props) {
  console.log(props);

  let weather = props.weather;
  let isGo = weather === "下雨" ? "不出门" : "出门";
  return (
      <div>
        <h1>函数式组件 hello world!</h1>
        <div>
          是否出门?
          <span>{isGo}</span>
        </div>
      </div>
  )
}
ReactDOM.render(<Childcom weather = "下雨"/>, document.querySelector("#root"));

组件里面套组件 - 复合组件

class HelloWorld extends React.Component {
    render() {
        return (
          <div>
              类组件 Hello World!
              <Childcom weather = "下雨"/>
          </div>
        )
    }
}
ReactDOM.render(<HelloWorld />, document.querySelector("#root"));

总结

函数式与类组件的区别和使用

  • 函数式组件比较简单,一般用于静态没有交互事件内容的组件页面
  • 类组件,一般又称为动态组件,那么一般会有交互或者数据修改的操作
  • 组件中又有组件叫做复合组件,复合组件中既可以有类组件又可以有函数组件
  • 函数式组件可以实现的功能,类一般能实现,类能实现的功能,函数式组件不一定能实现

相关文章

  • 1-5 函数式组件

    demo1 - 函数式组件 demo2 - 类组件 demo3 - 函数式组件传参 组件里面套组件 - 复合组件 ...

  • React 函数式组件

    简单函数式组件 使用 hook 的函数式组件

  • React入门(二)

    组件 1.函数式组件 什么是函数式组件创建一个函数,只要函数中返回一个新的JSX元素,则为函数式组件 调用组件可以...

  • React - 类组件创建

    React创建组件有两种方式 函数式组件 类组件函数式组件已经学过,现在看下类组件怎么写。 函数式组件和类组件区别...

  • React 面向组件编程

    函数式组件// 创建函数式组件function MyComponent() {console.log(this)/...

  • 函数式组件

    函数式组件 函数式组件,即无状态,无法实例化,内部没...

  • Render渲染函数和JSX

    render函数 h( 元素,属性,值 ) 中 h 不能少 使用 list组件中调用 函数式组件 定义函数式组件 ...

  • recompose函数式库 + ( git? ) + ( vo

    (一) recompose 函数式组件,高阶组件库 (1) withState 因为函数式组件中没有state,但...

  • Vue.js 2函数式组件学习

    什么是函数式组件? 函数式组件就是函数是组件,组件是函数,它的特征是没有内部状态、没有生命周期钩子函数、没有thi...

  • 笔记-React-Hooks

    一、矛与盾的问题?(Class组件与函数式组件)   在 React 中 Class 组件好用还是函数式组件好用呢...

网友评论

      本文标题:1-5 函数式组件

      本文链接:https://www.haomeiwen.com/subject/etlrcktx.html