如何向组件内部动态传入带内容的结构(标签)?
Vue中:
使用slot技术, 也就是通过组件标签体传入结构
React中:

  • 使用children props: 通过组件标签体传入结构
  • 使用render props: 通过组件标签属性传入结构,而且可以携带数据,一般用render函数属性

正常的父子组件是这样写的:

  1. import React, { Component} from 'react';
  2. import './components/style.css'
  3. class App extends Component {
  4. state = {
  5. count:10
  6. }
  7. render() {
  8. const { count} = this.state;
  9. return (
  10. <div className='parent'>
  11. <h2>我是APP组件</h2>
  12. <h3>count当前的值是:{count}</h3>
  13. <hr/>
  14. <Child/>
  15. </div>
  16. );
  17. }
  18. }
  19. class Child extends Component {
  20. render() {
  21. console.log('子组件调用render方法')
  22. return (
  23. <div className='child'>
  24. <h2>我是Child组件</h2>
  25. </div>
  26. );
  27. }
  28. }
  29. export default App;

1. children props

通过this.props.children接收父组件传过来的值。

  1. import React, { Component} from 'react';
  2. import './components/style.css'
  3. class App extends Component {
  4. state = {
  5. count:10
  6. }
  7. render() {
  8. const { count} = this.state;
  9. return (
  10. <div className='parent'>
  11. <h2>我是APP组件</h2>
  12. <h3>count当前的值是:{count}</h3>
  13. <hr/>
  14. <Child>
  15. hello,我是父组件传过来的值
  16. </Child>
  17. </div>
  18. );
  19. }
  20. }
  21. class Child extends Component {
  22. render() {
  23. console.log('子组件调用render方法')
  24. return (
  25. <div className='child'>
  26. <h2>我是Child组件</h2>
  27. <h4>{this.props.children}</h4>
  28. </div>
  29. );
  30. }
  31. }
  32. export default App;
  1. import React, { Component} from 'react';
  2. import './components/style.css'
  3. class App extends Component {
  4. state = {
  5. count:10
  6. }
  7. render() {
  8. const { count} = this.state;
  9. return (
  10. <div className='parent'>
  11. <h2>我是APP组件</h2>
  12. <h3>count当前的值是:{count}</h3>
  13. <hr/>
  14. <Child>
  15. <Son/>
  16. </Child>
  17. </div>
  18. );
  19. }
  20. }
  21. class Child extends Component {
  22. render() {
  23. return (
  24. <div className='child'>
  25. <h2>我是Child组件</h2>
  26. <h4>{this.props.children}</h4>
  27. </div>
  28. );
  29. }
  30. }
  31. class Son extends Component {
  32. render() {
  33. return (
  34. <div className='son'>
  35. <h2>我是Son组件</h2>
  36. </div>
  37. );
  38. }
  39. }
  40. export default App;

image.png

2. render Props

  1. import React, { Component} from 'react';
  2. import './components/style.css'
  3. class App extends Component {
  4. state = {
  5. count:10
  6. }
  7. render() {
  8. const { count} = this.state;
  9. return (
  10. <div className='parent'>
  11. <h2>我是APP组件</h2>
  12. <h3>count当前的值是:{count}</h3>
  13. <hr/>
  14. <Child render={() => <Son/>}/>
  15. </div>
  16. );
  17. }
  18. }
  19. class Child extends Component {
  20. render() {
  21. return (
  22. <div className='child'>
  23. <h2>我是Child组件</h2>
  24. {this.props.render()}
  25. </div>
  26. );
  27. }
  28. }
  29. class Son extends Component {
  30. render() {
  31. return (
  32. <div className='son'>
  33. <h2>我是Son组件</h2>
  34. </div>
  35. );
  36. }
  37. }
  38. export default App;

image.png

  1. import React, { Component} from 'react';
  2. import './components/style.css'
  3. class App extends Component {
  4. state = {
  5. count:10
  6. }
  7. render() {
  8. const { count} = this.state;
  9. return (
  10. <div className='parent'>
  11. <h2>我是APP组件</h2>
  12. <h3>count当前的值是:{count}</h3>
  13. <hr/>
  14. <Child render={(name, age) => <Son name={name} age={age}/>}/>
  15. </div>
  16. );
  17. }
  18. }
  19. class Child extends Component {
  20. state = {
  21. name:'IRIC',
  22. age:23
  23. }
  24. render() {
  25. const {name, age} = this.state
  26. return (
  27. <div className='child'>
  28. <h2>我是Child组件</h2>
  29. {this.props.render(name, age)}
  30. </div>
  31. );
  32. }
  33. }
  34. class Son extends Component {
  35. render() {
  36. return (
  37. <div className='son'>
  38. <h2>我是Son组件</h2>
  39. <h3>姓名:{this.props.name}</h3>
  40. <h3>年龄:{this.props.age}</h3>
  41. </div>
  42. );
  43. }
  44. }
  45. export default App;