代码之家  ›  专栏  ›  技术社区  ›  Alter

react native-无效的prop子级

  •  0
  • Alter  · 技术社区  · 8 年前

    我已经创建了一些组件。其中一个应该允许嵌套,但它莫名其妙地没有(莫名其妙,因为我找不到任何与这个问题相当的帖子)

    运行此操作需要一个映像(可以用任何内容替换)

    import React, {Component} from 'react';
    import PropTypes from 'prop-types';
    import { View, StatusBar, StyleSheet, ImageBackground } from 'react-native';
    
    export class PhonyStatusBar extends Component {
      render () {
        return (
            <View style={styles.statusBar} />
        );
      }
    }
    
    export class HomeScreen extends Component {
      static propTypes = {
          children: PropTypes.string
      }
    
      render () {
          return (
            <View>
                {this.props.children}
            </View>
          );
      }
    }
    
    export class AppGrid extends Component {
      render () {
        return (
          <View />
        );
      }
    }
    
    export default class App extends Component {
      render() {
        return (
          <View>
            {/* hide system status bar */}
            <StatusBar hidden={true} />
            <PhonyStatusBar />
    
            {/* throw in our own status bar  */}
            <HomeScreen> 
              <View />
            </HomeScreen>
          </View>
        );
      }
    }
    
    const styles = StyleSheet.create({
      backgroundImage: {
          width:'100%',
          height:'100%',
      },
      statusBar: {
        width: '100%',
        height: 25.33,
        backgroundColor: 'rgba(255, 157, 0, 0.5)'
      },
    });
    
    1 回复  |  直到 8 年前
        1
  •  1
  •   Dacre Denny    8 年前

    反应中 children 是组件的内置属性,已由库定义。这不是您应该手动定义的道具。有关详细信息,请参见: https://reactjs.org/docs/jsx-in-depth.html#children-in-jsx

    尝试删除:

     static propTypes = {
          children: PropTypes.string
      }
    

    HomeScreen 来解决这个问题。

    推荐文章