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
By default, the table title is horizontally cente...
The specific code is as follows: /*Scroll bar wid...
1 Introduction After "Maven deploys Springbo...
Preface The optional chaining operator (?.) allow...
11. Use JavaScript to create page effects 11.1 DO...
1 Installation Download the corresponding unzippe...
Table of contents 1.DB,DBMS,SQL 2. Characteristic...
Here I use samba (file sharing service) v4.9.1 + ...
The database installation tutorial of MySQL-8.0.2...
When we develop a single-page application, someti...
As shown above, padding values are composite at...
Table of contents Preface Prepare Implementation ...
1 method is a property that specifies how data is ...
webkit scrollbar style reset 1. The scrollbar con...
1: Install mongodb in docker Step 1: Install mong...