简体   繁体   English

无法访问 React js 组件的传递值

[英]Can't access React js component's passed down value

I'm fairly new to react and i would like to know why i can't access my props that are passed down.我对反应还很陌生,我想知道为什么我不能访问我传下来的道具。

In my StatContentBet.js Component, i cannot access the props object values which i passed down from StatContent.js.在我的 StatContentBet.js 组件中,我无法访问从 StatContent.js 传递下来的道具 object 值。

I can access this.props.img, but not this.props.bet.img, even though this.props.bet is a valid object.我可以访问 this.props.img,但不能访问 this.props.bet.img,即使 this.props.bet 是有效的 object。

https://gyazo.com/52347142134362b9cc4b7112c18ceaaf https://gyazo.com/52347142134362b9cc4b7112c18ceaaf

Here is my StatContentBet.js (Where i'm trying to call the passed down from)这是我的 StatContentBet.js (我试图从那里调用传下来的)

    import React from "react";
import MiniDice from "./MiniDice";
class StatsContentBet extends React.Component {

  render() {

    return (
        <div className="statsConentBet">
          <div className="betInfo">
            <img
              className="betInfo-userImg"
              src={this.props.img}
            />
            <span className="betInfo-username">Richard Henricks</span>
            <div className="betInfo-dices">
              {this.props.betChoices.map((betChoice, index) => {
                console.log(betChoice)
              })}
            </div>
          </div>
        </div>
    );
  }
}

export default StatsContentBet;

StatContent.js (Where i'm calling the StatContent.js(我在哪里调用

import React, { Component } from "react";
import StatContentBet from "./StatsContentBet";

class StatContent extends Component {
    constructor() {
        super(); 
        this.state = {

        };
    }    



    render() { 
       let bets =  [{
            id:1,
            username:"test",
            img: "https://miro.medium.com/max/1200/1*mk1-6aYaf_Bes1E3Imhc0A.jpeg",
            diceChoices: [1,2,3,4,5,6,1]
        },{
            id:2,
            username:"test",
            img: "https://miro.medium.com/max/1200/1*mk1-6aYaf_Bes1E3Imhc0A.jpeg",
            diceChoices: [1,2,3,4,5,6,1]
        },{
            id:3,
            username:"test",
            img: "https://miro.medium.com/max/1200/1*mk1-6aYaf_Bes1E3Imhc0A.jpeg",
            diceChoices: [1,2,3,4,5,6,1]
        },{
            id:4,
            username:"test",
            img: "https://miro.medium.com/max/1200/1*mk1-6aYaf_Bes1E3Imhc0A.jpeg",
            diceChoices: [1,2,3,4,5,6,1]
        }]

        return (
            <div id="statsContentContent">
                {/* <StatContentBet img='https://miro.medium.com/max/1200/1*mk1-6aYaf_Bes1E3Imhc0A.jpeg' /> */}
                {bets.map((bet, i) => 
                    <StatContentBet 
                        key={i} 
                        betChoices={bet.diceChoices} 
                        bet={bet} 
                        img={bet.img} 
                    />
                )}

            </div>
        )
    }
}

export default StatContent;

All the help is greatly appreciated!非常感谢所有帮助!

It's working fine for me https://jsfiddle.net/hawk939393/19a7bgxL/4/ ;对我来说工作正常https://jsfiddle.net/hawk939393/19a7bgxL/4/ I'm not getting any errors related to undefined .我没有收到任何与undefined相关的错误。

Ps you just have a typo in a component name StatContentBet -> StatsContentBet Ps 你只是在组件名称StatContentBet -> StatsContentBet中有错字

Edit: Make the following change inside your InputDices component:编辑:在InputDices组件中进行以下更改:

 StatsContentBet betChoices={[]}></StatsContentBet>

or you can check inside StatsContentBet whether this.props.betChoices is defined: if defined return jsx, else return something else return null或者您可以在this.props.betChoices内部检查StatsContentBet是否已定义:如果定义返回 jsx,否则返回其他内容return null

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM