React高级特性
创始人
2024-11-14 17:08:03
0

函数组件

import React from 'react'; // class组件 class ListClass extends React.Component {         constructor(props) {             super(props);     }      render() {             const {     list } = this.props;         return (             
    { list.map(item =>
  • item.id}>{ item.name}
  • )}
) } } // 函数组件 const ListFunc = (props) => { const { list } = props; return (
    { list.map(item =>
  • item.id}>{ item.name}
  • )}
) } export { ListClass, ListFunc }

函数组件

  • 纯函数,输入props,输出JSX
  • 没有实例,没有生命周期,没有state
  • 不能扩展其他方法

非受控组件

  • ref
  • defaultValue defaultChecked
  • 手动操作DOM元素
import React from 'react'; // class组件 class Test extends React.Component {         constructor(props) {             super(props);         this.fileInputRef = React.createRef();         this.nameInputRef = React.createRef();     }      render() {             return 
this.fileInputRef}/>
} alertFile = () => { const elem = this.fileInputRef.current; alert(elem.value); // 不是this.fileInputRef.value } alertName = () => { const elem = this.nameInputRef.current; alert(elem.value); // 不是this.nameInputRef.value } } export default Test;

非受控组件-使用场景

  • 必须手动操作DOM元素,setState实现不了
  • 文件上传
  • 某些富文本编辑器,需要传入DOM元素

如何选择

  • 优先使用受控组件,符合React设计原则
  • 必须操作DO,再使用受控组件

Portals

  • 组件默认会按照既定层次嵌套渲染
  • 如何让组件渲染到父组件以外?
import React from 'react' import ReactDOM from 'react-dom' import './style.css'  class App extends React.Component {         constructor(props) {             super(props)         this

相关内容

热门资讯

科普!金花链接房卡在哪买的,拼... 微信游戏中心:拼三张房卡,添加微信【33903369】,进入游戏中心或相关小程序,搜索“微信拼三张房...
科普!微信里玩金花房卡如何买,... 微信游戏中心:斗牛房卡,添加微信【8488009】,进入游戏中心或相关小程序,搜索“微信斗牛房卡”,...
科普!微信怎么开金花好友房间,... 微信游戏中心:斗牛房卡,添加微信【55051770】,进入游戏中心或相关小程序,搜索“微信斗牛房卡”...
科普!炸金花房间如何购买房卡,... 微信游戏中心:炸金花房卡,添加微信【33903369】,进入游戏中心或相关小程序,搜索“微信炸金花房...
科普!微信玩金花房卡怎么买,微... 微信游戏中心:牛牛房卡,添加微信【8488009】,进入游戏中心或相关小程序,搜索“微信牛牛房卡”,...
科普!微信群链接金花房卡,微信... 微信游戏中心:斗牛房卡,添加微信【55051770】,进入游戏中心或相关小程序,搜索“微信斗牛房卡”...
科普!微信里玩炸金花好友房怎么... 微信游戏中心:炸金花房卡,添加微信【33903369】,进入游戏中心或相关小程序,搜索“微信炸金花房...
科普!微信里面炸金花在哪买房卡... 微信游戏中心:炸金花房卡,添加微信【8488009】,进入游戏中心或相关小程序,搜索“微信炸金花房卡...
科普!创建炸金花微信链接房卡,... 微信游戏中心:炸金花房卡,添加微信【55051770】,进入游戏中心或相关小程序,搜索“微信炸金花房...
科普!微信玩金花链接房卡,微信... 微信游戏中心:牛牛房卡,添加微信【33903369】,进入游戏中心或相关小程序,搜索“微信牛牛房卡”...
科普!炸金花房间如何购买房卡,... 微信游戏中心:炸金花房卡,添加微信【8488009】,进入游戏中心或相关小程序,搜索“微信炸金花房卡...
科普!微信链接炸金花房卡怎么买... 微信游戏中心:炸金花房卡,添加微信【55051770】,进入游戏中心或相关小程序,搜索“微信炸金花房...
科普!微信玩金花游戏房卡在哪弄... 微信游戏中心:拼三张房卡,添加微信【33903369】,进入游戏中心或相关小程序,搜索“微信拼三张房...
科普!微信上炸金花房间买房卡,... 微信游戏中心:拼三张房卡,添加微信【8488009】,进入游戏中心或相关小程序,搜索“微信拼三张房卡...
科普!金花从哪里购买房卡,拼三... 微信游戏中心:拼三张房卡,添加微信【55051770】,进入游戏中心或相关小程序,搜索“微信拼三张房...
科普!金花房卡链接在哪弄的,微... 微信游戏中心:牛牛房卡,添加微信【33903369】,进入游戏中心或相关小程序,搜索“微信牛牛房卡”...
科普!如何购买炸金花房间房卡,... 微信游戏中心:拼三张房卡,添加微信【8488009】,进入游戏中心或相关小程序,搜索“微信拼三张房卡...
科普!微信上金花房间买房卡,拼... 微信游戏中心:拼三张房卡,添加微信【55051770】,进入游戏中心或相关小程序,搜索“微信拼三张房...
科普!炸金花链接房间卡怎么弄,... 微信游戏中心:炸金花房卡,添加微信【33903369】,进入游戏中心或相关小程序,搜索“微信炸金花房...
科普!微信上玩炸金花房间怎么开... 微信游戏中心:拼三张房卡,添加微信【8488009】,进入游戏中心或相关小程序,搜索“微信拼三张房卡...