React tutorial: Types of Props for child components
Usually, the subcomponents of a React component are a group, that is, the subcomponents are an array. See the following example
var GenericWrapper = React.createClass({
componentDidMount: function() {
console.log(Array.isArray(this.props.children)); // => true
},
render: function() {
return <div />;
}
});
ReactDOM.render(
<GenericWrapper><span/><span/><span/></GenericWrapper>,
document.getElementById('content')
);
The GenericWrapper component has three subcomponents , so the above example will print true in the console.
Although its subcomponent is an array, if its subcomponent has only one component, it does not mean that there is only one element in the array, but that this.props.children is not an array. In other words, if there is only itself and no subcomponents, it is not an empty array, and this. props .children itself is not an array.
var GenericWrapper = React.createClass({
componentDidMount: function() {
console.log(Array.isArray(this.props.children)); // => false
},
render: function() {
return <div />;
}
});
ReactDOM.render(
<GenericWrapper><span/></GenericWrapper>,
document.getElementById('content')
);
In other words, the above result will output false in the console.
However, there is one thing to note here: we are talking about the subcomponents of the component, not the child elements. We can get the subcomponents through this.props.children, but we cannot get the child elements.
var GenericWrapper = React.createClass({
componentDidMount: function() {
console.log(Array.isArray(this.props.children)); // => false
},
render: function() {
return <div><span/><span/></div>;
}
});
ReactDOM.render(
<GenericWrapper></GenericWrapper>,
document.getElementById('content')
);
Here, false is printed in the console. This is because in this example, span is a child element of div, and the GenericWrapper component has no child components. So this.props.children is not an array.
For a component with no child components, this.props.children is undefined.
var GenericWrapper = React.createClass({
componentDidMount: function() {
console.log(this.props.children); // => undefined
},
render: function() {
return <div><span/><span/></div>;
}
});
ReactDOM.render(
<GenericWrapper></GenericWrapper>,
document.getElementById('content')
);
The result printed out by the console is undefined.
The content of this article is relatively simple, and I hope it will be helpful to everyone.
For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.
Related Articles
React Tutorial - Transferring Props
Publish Date:2025/03/16 Views:185 Category:React
-
React transfers Props. Props are generated when components are encapsulated. Components expose some properties (Props) to the outside world to complete some functions.
React Tutorial: Props Anti-Pattern
Publish Date:2025/03/16 Views:183 Category:React
-
React's Props anti-pattern, using Props to generate state in getInitialState is an anti-pattern - Anti-Pattern.
React Tutorial - Props Validation
Publish Date:2025/03/16 Views:99 Category:React
-
Props validation is a very useful way to use components correctly. It can avoid many bugs and problems as your application becomes more and more complex. In addition, it can make your program more readable.
Rendered fewer hooks than expected error in React
Publish Date:2025/03/16 Views:50 Category:React
-
When we use a hook after a condition that may return a value, we get the error "Rendered fewer hooks than expected. This may be caused by an accidental early return statement". To fix this error, you need to move all React hooks to any condition that
Fix Uncaught ReferenceError: useState is not defined in React
Publish Date:2025/03/15 Views:142 Category:React
-
When we use the useState hook in our code but forget to import it, it generates the error Uncaught ReferenceError: useState is not defined. To fix this error, you need to import the hook before using it import {useState} from react . // ?️
React error Uncaught ReferenceError: process is not defined solution
Publish Date:2025/03/15 Views:117 Category:React
-
To resolve the “Uncaught ReferenceError: process is not defined” error in React, open a terminal in the root directory of your project and update the version of the `react-scripts` package by running `npm install react-scripts@latest` and reinstal
How to solve the "Function components cannot have string refs" error in React
Publish Date:2025/03/15 Views:139 Category:React
-
When we use a string as a reference in a function component, we get the error "Function components cannot have string refs". To fix this error, we need to use the useRef() hook to get a mutable ref object that we can use as a component.
React Error Property 'X' does not exist on type 'Readonly<{}>'
Publish Date:2025/03/15 Views:140 Category:React
-
The React.js error “Property does not exist on type 'Readonly'” occurs when we try to access the props or state of an untyped class component. To fix the error, you need to use generics on the React.Component class to type the props or state of th
Solve React's Unexpected default export of anonymous function error
Publish Date:2025/03/15 Views:120 Category:React
-
When we try to export an anonymous function using default export, it results in the warning “Unexpected default export of anonymous function.” To fix this error, you need to give the function a name before exporting it.