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 Features1. 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:
|
<<: Network configuration of Host Only+NAT mode under VirtualBox
>>: Installing Win10 system on VMware workstation 14 pro
Official website: http://code.google.com/p/zen-cod...
This article shares the specific code for JavaScr...
Illustrated CentOS 7.3 installation steps for you...
background Sometimes we need to get the creation ...
MySQL Advanced SQL Statements use kgc; create tab...
[Looking at all the migration files on the Intern...
1. Nested routing is also called sub-routing. In ...
Table of contents 1. Original value and reference...
Recently I need to change the version of MySQL da...
Table of contents 1. Demand 2. Solution 3. The fi...
Table of contents 1.Nuxt server-side rendering ap...
This article uses an example to describe how to u...
This article example shares the specific code of ...
01 Winter Flakes (Individual only) 02 Snowtop Cap...
Installing and deploying a private Docker Registr...