简体   繁体   English

在 React/Redux 应用程序中,为什么我的子组件中的 props.property 在控制台日志中显示未定义

[英]In React/Redux app why is my props.property in child component showing undefined in console log

I have built a Flashcard app and decided to implement Redux.我已经构建了一个抽认卡应用程序并决定实现 Redux。 On componentMount() it is running a getRandomCard action which sets the state of the randomCard.在 componentMount() 上,它正在运行一个 getRandomCard 操作,该操作设置 randomCard 的 state。 My redux state shows the correct state of the random card which changes randomly with each refresh however when I pass the props to a child card component to display the question, answer and graphic the props.randomCard.question etc shows as undefined.我的 redux state 显示随机卡的正确 state 每次刷新都会随机变化,但是当我将道具传递给未定义的子卡组件以显示问题、问题和答案等图形时I don't know why.我不知道为什么。 Here is the code for my reducer.这是我的减速器的代码。

 import CARD_DATA from '../components/card.data'

const INITIAL_STATE = {
  cards: CARD_DATA,
  randomCard: {},
  seenCard: [],
  endOfDeck: false
}

const cardReducer = function cards(state = INITIAL_STATE, action) {

  switch (action.type) {
    case 'GET_RANDOM_CARD': 
      return {
        ...state,
        randomCard: [action.currentCards[Math.floor((Math.random() * action.currentCards.length))]]
      }

    default: return state
  }
}


export default cardReducer

Here is the action:这是动作:

export function getRandomCard(currentCards) {
  return {
    type: 'GET_RANDOM_CARD',
    currentCards: currentCards,
  }
}
enter code here
enter code here

Here is the parent componentDidMount method:这是父 componentDidMount 方法:

class App extends Component {

  componentDidMount() {
    const {getRandomCard, cardData} = this.props
    getRandomCard(cardData)
  }

And here is the child component which is trying to render the props:这是尝试渲染道具的子组件:

return (
      <div className="card-container">
        <div className="card">
          <div className="front">
            <div className="question">{props.randomCard.question}</div>
            <div className="image">
              <img src={props.randomCard.imageUrl} alt='graphic not available' />
            </div>
          </div>
          <div className="back">
            <div className="answer">{props.randomCard.answer}</div>
          </div>
        </div>
      </div>
    )

}

export default Card

Props.randomCard console.logs this: Props.randomCard console.logs 这个:

0: {id: 3, question: "What is immutablity?", answer: "When an object or an array does not get mutated when being passed to a function", imageLink: "https://miro.medium.com/max/600/1*2N0l3bLqaBgmOSIay-uc5w.png", priority: 5}
length: 1
__proto__: Array(0)

I have tried accessing these props using props.randomCard.question and props.randomCard[0].question all to no avail and I am blocked.我尝试使用 props.randomCard.question 和 props.randomCard[0].question 访问这些道具都无济于事,我被阻止了。

I have figured out why I was getting props.randomCard[0].question undefined.我已经弄清楚为什么我会得到 props.randomCard[0].question 未定义。 The component was rendering before it received the props.该组件在收到道具之前正在渲染。

I fixed issue by adding the following conditional statement:我通过添加以下条件语句解决了问题:

render () {
  if (this.props.card[0]) {
  return (
  <div className="card-container">
    <div className="card">
      <div className="front">
        <div className="question">{this.props.card[0].question}</div>
        <div className="image">
          <img src={this.props.card[0].imageLink} alt='graphic not available' />
        </div>
      </div>
      <div className="back">
        <div className="answer">{this.props.card[0].answer}</div>
      </div>
    </div>
  </div>
)

} else {
  return null
}
}

} }

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

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