react传参

react传参

  1. 基本传参 props
    <child age={this.state.age}>
    在子组件中
    this.props.age 获取数据
    传参方法
setAge = v=>this.setState({age:v})
<child age={this.state.age} setAge={this.setAge. bind(this)}>

在子组件使用
<h3 onClick= {()=>this.props.setAge(15)}>

  1. 上下文传递参数 context
    <h3 onClick= {()=>this.props.setAge(15)}>
    所有引用数据的视图都会自动更新
父组件
导入类型检测

import PropTypes from 'prop-types';

定义导出的数据类型
static childContextTypes = {
        color:PropTypes.string, // 字符串类型
        setColor:PropTypes.func,// 方法类型
    }
获取需要传参的数据
getChildContext(){
    return{
        color:this.state.color,
        setColor:v=>this.setState({color:v})}
}

子孙组件

定义上下文数据类型
static contextTypes = {
        color:PropTypes.string, // 字符串类型
        setColor:PropTypes.func, //  方法类型
}
使用上下文数据

<h3 style={{color:this.context.cor}}>Child组件</h3>

使用上下文方法

"<button onClick={0=>this.context.setColor('gold')}>

  1. 上下文方式传递
    context
    provider Comsumer
定义上下文组件
import React from 'react'
let {Consumer,Provider} = React.createContext();
//创建一个上下文 结构 Consumer 消费者,Provider 供应商
export {Consumer,Provider}
//导出
父组件

1.导入
import {Provider} from ' ./context'
2.用provider包裹子元素并传递value数据

<Provider value={{
  num:this.state.num,
   setNum:this.setNum
}}>

3.子组件中

导入

import { Consumer} from './context' //导入消费者组件

  1. Comsumer中的context获取数据
<Consumer>
  {context=>(
    <div><h3>
      <button onClick={()=>context.setNum(context.num+1)}>{context.num}</button>
    </h3></div>)
  }
</Consumer> 
  1. redux react-redux
    全局数据状态共享
    vuex就是有参考redux的
  • 安装
    npm i redux react-redux
  • 从 react-redux导入Provider
    import {Provider} from 'react-redux';
  • 把根组件替换为
<Provider>
     <App/>
</Provider>
  • 在Provider中添加数据仓库
    <Provider store={store}>
  • 编写store仓库并导入仓库
  • 编写store
    1.redux导入
    2.reducer 初始数据方法
    3.actions 处理数据动作
    4.导出仓库
  • 在组件中使用
    1.导入连接
    2.导出组件
export default connect(mapStateToProps,mapDisPatchToProps)(Detail)
mapStateToProps    //组state数据映射为 组件的props
mapDisPatchToProps   //把state中的方法映射诶porps中的方法

react渲染html内容

<div dangerouslySetInnerHTML={{__html:item.msg}}></div>

富文本 编译器

  1. 需要插件
    npm i react-draft-wysiwyg draftjs-to-html draft-js -S
  2. 导入
import {Editor} from 'react-draft-wysiwyg';
import 'react-draft-wysiwyg/dist/react-draft-wysiwyg.css'
import draftjs from 'draftjs-to-html'
  1. 使用
<Editor
    editorState={this.state.editorState}
    // 绑定编辑状态
    onContentStateChange={this.onContentStateChange}
    // 当内容发生改变时候
    onEditorStateChange={this.onEditorStateChange}
    // 当编辑器状态发生改变的时候 
></Editor>
  1. 真正的内容
    draftjs(this.state.editorContent)
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 基本传参props 传递数据 <child age={this.state.age}> 在子组件中 this.pr...
    char1阅读 278评论 0 0
  • 深入JSX date:20170412笔记原文其实JSX是React.createElement(componen...
    gaoer1938阅读 8,102评论 2 35
  • 3. JSX JSX是对JavaScript语言的一个扩展语法, 用于生产React“元素”,建议在描述UI的时候...
    pixels阅读 2,873评论 0 24
  • [toc] REACT react :1.用来构建用户界面的 JAVASCRIPT 库2.react 专注于视图层...
    拨开云雾0521阅读 1,469评论 0 1
  • 今天的React题没有太多的故事…… 半个月前出了248个Vue的知识点,受到很多朋友的关注,都强烈要求再出多些R...
    浪子神剑阅读 10,121评论 6 106