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

Zen coding resource update function enhancement

Official website: http://code.google.com/p/zen-cod...

JavaScript imitates Taobao magnifying glass effect

This article shares the specific code for JavaScr...

Graphical tutorial on installing CentOS 7.3 on VMWare

Illustrated CentOS 7.3 installation steps for you...

Getting the creation time of a file under Linux and a practical tutorial

background Sometimes we need to get the creation ...

Some common advanced SQL statements in MySQL

MySQL Advanced SQL Statements use kgc; create tab...

Docker cleaning killer/Docker overlay file takes up too much disk space

[Looking at all the migration files on the Intern...

Vue implements nested routing method example

1. Nested routing is also called sub-routing. In ...

JavaScript Advanced Programming: Variables and Scope

Table of contents 1. Original value and reference...

Why is there this in JS?

Table of contents 1. Demand 2. Solution 3. The fi...

A brief discussion on four solutions for Vue single page SEO

Table of contents 1.Nuxt server-side rendering ap...

mysql8 Common Table Expression CTE usage example analysis

This article uses an example to describe how to u...

jQuery canvas draws picture verification code example

This article example shares the specific code of ...

33 ice and snow fonts recommended for download (personal and commercial)

01 Winter Flakes (Individual only) 02 Snowtop Cap...

Implementation of Docker private library

Installing and deploying a private Docker Registr...