React non-parent-child component parameter passing example code

React non-parent-child component parameter passing example code

React is a JAVASCRIPT library for building user interfaces.

React is mainly used to build UI, and many people think that React is the V (view) in MVC.

React originated as an internal project at Facebook, used to build Instagram's website, and was open sourced in May 2013.

React has high performance and very simple code logic, and more and more people have begun to pay attention to and use it.

React Features

1. Declarative design − React uses a declarative paradigm that makes it easy to describe applications.

2. Efficiency − React minimizes the interaction with DOM by simulating DOM.

3. Flexible − React works well with known libraries or frameworks.

4.JSX − JSX is an extension of JavaScript syntax. Using JSX is not required for React development, but it is recommended.

5. Components − Building components with React makes the code easier to reuse and can be well applied in the development of large projects.

6. One-way responsive data flow − React implements one-way responsive data flow, which reduces duplication of code, which is why it is simpler than traditional data binding.

Here is an example code for React non-parent-child component parameter passing. The specific content is as follows:

New version: The main purpose of cross-level parameter passing is to avoid assigning values ​​at each level and also avoid using dva

import React from 'react'
const {Provider, Consumer} = React.createContext('default')
export default class ContextDemo extends React.Component {
    state={
        newContext:'createContext'
    }
  render() {
      const { newContext } = this.state
    return (
        <Provider value={newContext}>
            <div>
                <label>childContent</label>
                <input type="text" value={newContext} onChange={e=>this.setState({newContext:e.target.value})}/>
            </div>
            <Son />
        </Provider>
    )
  }
}
class Son extends React.Component{
    render(){
        return <Consumer>
            {
                (name)=>
                    <div style={{border:'1px solid red',width:'60%',margin:'20px auto',textAlign:'center'}}>
                        <p>Content obtained by the subcomponent: {name}</p>
                        <Grandson />
                    </div>
                
            }
        
        </Consumer>
    }
}
class Grandson extends React.Component{
    render(){
        return <Consumer>
            {
                (name)=>
                    <div style={{border:'1px solid red',width:'60%',margin:'20px auto',textAlign:'center'}}>
                        <p>The content obtained by the grandchild component: {name}</p>
                    </div>
                
            }
        
        </Consumer>
    }
}

The old project method is also introduced, using prop-types

import React from 'react'
import PropTypes from 'prop-types'
class ContextDemo extends React.Component {
    // getChildContext
    state={
        newContext:'createContext'
    }
    getChildContext(){
        return {value:this.state.newContext}
    }
  render() {
      const { newContext } = this.state
    return (
            <div>
                <div>
                    <label>childContent</label>
                    <input type="text" value={newContext} onChange={e=>this.setState({newContext:e.target.value})}/>
                </div>
                <Son />
            </div>
    )
  }
}
class Son extends React.Component{
    render(){
        return <div>
            <p>children:{this.context.value}</p>
        </div>
    }
}
Son.contextTypes = {
    value:PropTypes.string
}
ContextDemo.childContextTypes = {
    value:PropTypes.string
}
export default () => 
  <ContextDemo>
  
  </ContextDemo>

ref

import React from 'react'

// Function components also want to get dom through ref
const TargetFunction = React.forwardRef((props, ref) => (
    <input type="text" ref={ref}/>
))
export default class FrodWordRefDemo extends React.Component {
  constructor() {
    super()
    this.ref = React.createRef()
  }

  componentDidMount() {
    this.ref.current.value = 'ref get input'
  }

  render() {
    return <TargetFunction ref={this.ref}>
    </TargetFunction>
  }
}

pubsub-js

import React from 'react'
import PubSub from 'pubsub-js'
export default class Bro extends React.Component{
    state = {
        value:''
    }

    render(){
        const {value} = this.state
        PubSub.subscribe('SOS',(res,data)=>{
            this.setState({
                value:data
            })
        })
        return (
            <div>
                I received <h1>{value}</h1>
            </div>
        )
    }
}
import React from 'react'
import PubSub from 'pubsub-js'
export default class Children extends React.Component{
    state = {
        value:''
    }
    handelChange = (e) => {
        this.setState({
            value:e.target.value
        })
    }
    send = () => {
        const {value} = this.state
        PubSub.publish('SOS',value)
    }
    render(){
        const {value} = this.state
        return (
            <div>
                <input type="text" value={value} onChange={this.handelChange}/>
                <button onClick={this.send}>Send</button>
            </div>
        )
    }
}

This is the end of this article about the example code of React non-parent-child component parameter passing. For more relevant React non-parent-child component parameter passing content, please search 123WORDPRESS.COM's previous articles or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • About the value transfer problem between antd tree and parent-child components (react summary)
  • React parent-child component communication implementation method
  • React method of passing values ​​between parent and child components
  • React communication props between parent and child components
  • Detailed explanation of React's parent-child component transfer and some other key points
  • react.js parent-child component data binding real-time communication example code
  • React parent-child component value transfer (component communication) implementation method

<<:  Network configuration of Host Only+NAT mode under VirtualBox

>>:  Installing Win10 system on VMware workstation 14 pro

Recommend

HTML table markup tutorial (16): title horizontal alignment attribute ALIGN

By default, the table title is horizontally cente...

Example code for css3 to achieve scroll bar beautification effect

The specific code is as follows: /*Scroll bar wid...

Detailed tutorial on deploying Springboot or Nginx using Kubernetes

1 Introduction After "Maven deploys Springbo...

Use of js optional chaining operator

Preface The optional chaining operator (?.) allow...

Use JavaScript to create page effects

11. Use JavaScript to create page effects 11.1 DO...

Unzipped version of MYSQL installation and encountered errors and solutions

1 Installation Download the corresponding unzippe...

MySQL learning database backup detailed explanation

Table of contents 1.DB,DBMS,SQL 2. Characteristic...

Detailed explanation of samba + OPENldap to build a file sharing server

Here I use samba (file sharing service) v4.9.1 + ...

MySQL 8.0.22 winx64 installation and configuration method graphic tutorial

The database installation tutorial of MySQL-8.0.2...

Detailed explanation of 8 ways to pass parameters in Vue routing components

When we develop a single-page application, someti...

Detailed explanation of padding and abbreviations within the CSS box model

As shown above, padding values ​​are composite at...

How to elegantly implement WeChat authorized login in Vue3 project

Table of contents Preface Prepare Implementation ...

Introduction to the method attribute of the Form form in HTML

1 method is a property that specifies how data is ...

Implementation of CSS scroll bar style settings

webkit scrollbar style reset 1. The scrollbar con...

Detailed steps to install Docker mongoDB 4.2.1 and collect springboot logs

1: Install mongodb in docker Step 1: Install mong...