【问题标题】:How i can destructuring {this.props.children}?我如何解构 {this.props.children}?
【发布时间】:2019-05-25 15:55:18
【问题描述】:

我想为我的移动应用程序添加背景,但是当我使用“this.props.children”时 eslint 说我“必须使用解构道具分配”。为什么我可以解构这个道具?

这是我的代码,

export default class WallPaper extends Component {
  render() {
    return (
      <ImageBackground
        source={backgroundimg}
        imageStyle={{ opacity: 0.9 }}
        style={styles.container}
      >
        {this.props.children}
      </ImageBackground>
    );
  }
}

当我使用这个代码时

export default class WallPaper extends Component {
  render() {
    const { children } = this.props;
    return (
      <ImageBackground
        source={backgroundimg}
        imageStyle={{ opacity: 0.9 }}
        style={styles.container}
      >
        {children}
      </ImageBackground>
    );
  }
}

我有这个错误“道具验证中缺少'孩子'”

当我使用这段代码时,

export default class WallPaper extends Component {
  render() {
     (this.props) => {
    return (
      <ImageBackground
        source={backgroundimg}
        imageStyle={{ opacity: 0.9 }}
        style={styles.container}
      >
        {props.children}
      </ImageBackground>
    );
    }
  }
}

我有这个错误,

提前感谢您的帮助!

【问题讨论】:

标签: javascript node.js reactjs react-native destructuring


【解决方案1】:

对于类组件:

export class Welcome extends Component {
    render(){
        const {name, hero, children} = this.props
        return (
            <div>
                <h1> Class Component with {name} as {hero} </h1>
                {children}
            </div>
            )
    }
}

【讨论】:

    【解决方案2】:

    缺少功能组件案例的答案。 children 只是传递给组件的道具。为了像使用 props.url 一样使用它,您需要将其添加到该列表中,以便可以从 props 对象中“拉出”。

    export const Welcome = ({name, hero, children}) => {
            return (
            <div>
               <h1> Class Component with {name} as {hero} </h1>
                {children}
            </div>
            )
        }
    

    【讨论】:

      【解决方案3】:

      您可以尝试以下方法从this.props 解构children

      export default class WallPaper extends Component {
        render() {
          const { children } = this.props;
      
          return (
            <ImageBackground
              source={backgroundimg}
              imageStyle={{ opacity: 0.9 }}
              style={styles.container}
            >
              {children}
            </ImageBackground>
          );
        }
      }
      

      看起来您的项目可能需要此组件的 propTypes。尝试以下添加 children 道具类型。注意,你需要安装包prop-types:

      // ... 
      import PropTypes from 'prop-types';
      
      class WallPaper extends Component {      
        render() {
          const { children } = this.props;
      
          return (
            <ImageBackground
              source={backgroundimg}
              imageStyle={{ opacity: 0.9 }}
              style={styles.container}
            >
              {children}
            </ImageBackground>
          );
        }
      }
      
      WallPaper.propTypes = {
        children: PropTypes.node // or PropTypes.node.isRequired to make it required
      };
      
      export default WallPaper;
      

      希望对您有所帮助!

      【讨论】:

      • 我已经尝试过这个方法,但是我得到错误“'children' is missing in props validation”
      • @MichelMelhem const { children } = this.props; 正是您实现解构的方式。如果您对组件 WallPaper 进行了 PropTypes 验证,请参阅此 answer 了解子 PropTypes 验证的方法。尝试首先暂时删除 propTypes 以查看是否解决了通过此解构分配的 linting 错误。如果在将子项添加到 propTypes 验证后仍然存在问题,请分享您的 propTypes 代码以获得更多支持。谢谢!
      • 您的项目是否要求每个组件都有propTypes?如果是这样,请继续并将 propTypes 添加到此组件。
      【解决方案4】:
      export default class WallPaper extends Component {
        render() {
           (this.props) => {
          return (
            <ImageBackground
              source={backgroundimg}
              imageStyle={{ opacity: 0.9 }}
              style={styles.container}
            >
              {props.children}
            </ImageBackground>
          );
          }
        }
      }
      

      【讨论】:

      • 当我使用你的代码时,我遇到了解析错误:Unexpected token 15 |渲染() { 16 | props => ( > 17 | return ( | ^ 18 |
      【解决方案5】:

      这是由于linting rule

      如果不想破坏,可以关闭规则。

      如果你愿意,你可以这样做。

      export default class WallPaper extends Component {
        render() {
        const {children} = this.props
          return (
            <ImageBackground
              source={backgroundimg}
              imageStyle={{ opacity: 0.9 }}
              style={styles.container}
            >
              {this.props.children}
            </ImageBackground>
          );
        }
      }
      

      【讨论】:

      • @MichelMelhem 什么??你会用英文写吗?看不懂你写了什么?
      • 对不起,失败了,这是消息:是的,我知道我可以禁用此规则,但如果存在此规则,这可能是一个很好的理由
      猜你喜欢
      • 2016-06-20
      • 2019-01-07
      • 2020-07-03
      • 2017-04-02
      • 2016-06-01
      • 2019-11-12
      • 1970-01-01
      相关资源
      最近更新 更多