In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
Today, the editor will share with you the relevant knowledge points about how to achieve react communication. The content is detailed and the logic is clear. I believe most people still know too much about this knowledge, so share this article for your reference. I hope you can get something after reading this article. Let's take a look at it.
React communication methods include: 1, using props for parent-child component communication; 2, using callback function for child parent component communication; 3, using variable promotion for sibling component communication; 4, using Context for cross-component communication; 5, using node events module for singleton communication; 6, using redux to share data communication.
The operating environment of this tutorial: Windows7 system, react17.0.1 version, Dell G3 computer.
Six Communication modes of React
1.props father-son communication
two。 Callback function, child-parent communication
3. Variable promotion, sibling component communication
4.Context, cross-component communication
Singleton Communication of events Module of 5.node
6.redux shared data communication
1.props father-son communication
Function Children (props) {return (
Children
{props.text}
)} function Parent () {return (
Parent
)} export default Parent
two。 Callback function, child-parent communication
Function Children (props) {return (
Children
{props.text}
{props.handleChange ('changed')}} > Click me to change what my father passed on to me)} function Parent () {let [text, setText] = useState ('this is what Dad passed to you') function handleChange (val) {setText (val)} return (
Parent
)} export default Parent
3. Variable improvement, brothers set up correspondence
Function Children (props) {return (
Children
{props.setText ('I am a message from Children')} > send a message to Children1)} function Children1 (props) {return (
Children1
{props.text}
)} function App () {let [text, setText] = useState ('') return (APP)} export default App
4.Context, cross-organizational communication
Old writing method
Class Children extends React.Component {static contextTypes = {text: PropsType.string} render () {return (
Children
{this.context.text}
)} class Parent extends React.Component {static childContextTypes = {text: PropsType.string} getChildContext () {return {text:'I'm the father's message'}} render () {return (
Parent
)} export default Parent
New writing method
Const {Consumer, Provider} = React.createContext () class Children extends React.Component {render () {return ({(value) = > (
Children1
{value.text}
)} class Parent extends React.Component {render () {return (
Parent
)} export default Parent
Singleton Communication of events Module of 5.node
Function Children (props) {return (
Children
{props.text}
{props.event.emit ('foo')}} > Click me to change what my father passed to me)} function Parent () {let [text, setText] = useState (' this is what Dad passed to you') let event = new Events () event.on ('foo', () = > {setText (' changed'}) return (
Parent
)} export default Parent
Note ⚠️: this communication remembers to introduce the events module at the top, without installation, node its own module.
6.redux shared data communication
Store.js
Import {createStore} from 'redux'let defaultState = {text:' I am store'} let reducer = (state = defaultState, action) = > {switch (action) {default: return state}} export default createStore (reducer)
Child.js
Import React from 'react'import {connect} from' react-redux'class Child extends React.Component {render () {return (Child)
{this.props.text}
)} let mapStataToProps = (state) = > {return {text: state.text}} export default connect (mapStataToProps, null) (Child)
Parent.js
Class Parent extends React.Component {render () {return (
Parent
)} export default Parent
Note: remember to install redux and react-redux.
These are all the contents of this article entitled "how to achieve react Communication". Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 217
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.