react中的组件嵌套

组件嵌套:将子组件以标签的形式写在父组件的模板中。

组件之间的通信

子传父

子传父

通过函数层层传递

 

 

 

 

 

 

点击h3 执行fn fn中去执行onlick函数而onlick是来自于props的,props中的onlick又是ff,转移去执行ff把参数赋给a 修改了a的值。

父传子

 

.使用props传值

 

具体实现

 

import React, { Component } from 'react'; /**父组件 */ export default class Parent extends Component { state = { msg: 1 } render() { return ( <div onClick={() => this.setState({ msg: this.state.msg + 1 })} > {/* 子组件 */} <Child msg={"传递给子组件的消息:" + this.state.msg.toString()} /> </div> ); } } /**子组件 */ class Child extends Component { // 默认的props 可写可不写 static defaultProps = { msg: 1 } render() { return ( <div> {/* 通过props传递过来的参数 */} {this.props.msg} </div> ) } }

 

转载于:https://www.cnblogs.com/yangjingyang/p/11574802.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
虽然tiptap是基于Vue.js的,但也可以在React使用。在React嵌套组件的方法与Vue.js类似,使用NodeView和MarkView来实现。 NodeView和MarkView是针对Vue.js的特定组件,但是您可以使用类似的方法将它们转换为React组件。这需要一些额外的工作,但是理论上是可行的。 以下是在React使用tiptap嵌套组件的示例: ```javascript import { NodeView } from 'tiptap' import React from 'react' import ReactDOM from 'react-dom' class CustomLink extends React.Component { render() { const { url, children } = this.props return ( <a href={url} style={{ color: 'red' }}> {children} </a> ) } } class CustomLinkNodeView extends NodeView { constructor(node, view, getPos) { super(node, view, getPos) this.dom = document.createElement('a') } update(node) { if (node.type.name !== this.node.type.name) { return false } this.dom.setAttribute('href', node.attrs.url) ReactDOM.render( <CustomLink url={node.attrs.url}> {node.textContent} </CustomLink>, this.dom ) return true } } export default CustomLinkNodeView ``` 在上面的示例,我们定义了一个名为CustomLink的React组件,并使用NodeView将其映射到一个文本节点。CustomLink组件接受一个url属性,并将节点文本作为其子元素渲染。在CustomLinkNodeView类,我们使用ReactDOM.render方法将CustomLink组件渲染到this.dom元素。最后,我们将CustomLinkNodeView导出为默认值。 请注意,在React使用tiptap嵌套组件需要进行一些额外的工作,因为tiptap是针对Vue.js的。但是,如果您熟悉React并且不想使用Vue.js,这是一种可行的方法。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值