Parent and child components pass values
Parent component passes value to child component
- The parent component provides what to pass
The state data
- Give child components
Add tag Attributes
.value
isData in state
- Child component pass
props
Receives data from the parent component
class P extends React.Component {
state = {
childv: 'Parent passes value to child'
}
render() {
return (
<div>
<C childValue={this.state.childv} />
</div>)}}class C extends React.Component {
render() {
return (
<div>
{this.props.childValue}
</div>
)
}
}
ReactDOM.render(<P />.document.getElementById('root'))
Copy the code
Child components pass values to parent components
The general idea: using the callback function, the parent component provides the callback function, and the child component calls the data to be passed as arguments to the callback function
- The parent component
Provides a callback function
forReceive data
- Will the
function
As aAttribute values
And passed to theChild components
- Child component pass
props
Receive andCall the callback function
- Subcomponent
data
As aparameter
Passed to theThe callback function
class P extends React.Component {
getMessage = data= > {
console.log('Parent component receives data', data)
}
render() {
return (
<div>
<C getM={this.getMessage} />
</div>)}}class C extends React.Component {
state = {
msg: 'Child passes data to parent'
}
handleMessage = () = > {
this.props.getM(this.state.msg)
}
render() {
return (
<div>
<button onClick={this.handleMessage}>Click on the</button>
</div>
)
}
}
ReactDOM.render(<P />.document.getElementById('root'))
Copy the code
Sibling components pass values
The general idea: Promote state sharing to the nearest common parent, which manages state
- ascension
State of the public
- provide
Operation Shared state
themethods
Click the button to count. Button for counting operation, numbers for display
-
The shared state is: numbers
-
To manipulate the shared state, click the button and proceed with the number +1
class P extends React.Component {
// Share status
state = {
count: 0
}
// A method to manipulate the shared state
add = () = > {
this.setState({
count: this.state.count + 1})}render() {
return (
<div>
<C1 count={this.state.count} ></C1>
<C2 add={this.add} ></C2>
</div>)}}// Data display
const C1 = (props) = > {
return (
<div>{props.count}</div>)}// Logical operation
const C2 = (props) = > {
return (
<div>
<button onClick={()= >{props. Add ()}}> I'm button +1</button>
</div>
)
}
ReactDOM.render(<P />.document.getElementById('root'))
Copy the code
Value transfer for grandparent and grandparent components
Context passes data across components
- call
React.createContext()
createProvider(provides data)
andConsumer data
Two components
const { Provider, Consumer } = React.createContext()
Copy the code
- use
The Provider component acts as the parent node
<Provider>
<div className="App">
<Child/>
</Provider>
Copy the code
- Set up the
The value attribute
And said toData passed
<Provider value="Ancestor component passes data">
Copy the code
- Calling the Consumer component
Receive data
(In the Consumer component,The callback function
theparameter
, it isParameters passed by the Provider
)
<Consumer>
{data= > <span>{data}</span>}
</Consumer>
Copy the code
conclusion
-
Functional components (stateless components) can access data through props. Class components (stateful components) fetch data via this.props
-
Const A = ()=>{} const A = ()=>{} In the event binding, the combination of function expressions and arrow functions can omit the this binding
-
Const {MSG} = this.state (const {MSG} = this.state)
-
Text description of callback and arrow functions. The arrow function is recognized in the code as the arrow function, but the callback function is to delay execution until needed. In this article, arrow functions are used to delay execution, so some arrow functions are called callback functions to better understand the execution logic of component code