Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
151 views
in Technique[技术] by (71.8m points)

reactjs - Passing Property To Another Component - React

I need to pass "notecards" (an array) down from "Notecard.js" to "LoadQuestions.js". Console log shows that it is passing, but when I use {notecards} within the "return" it errors as "undefined". Could you please take a look?

Notecard.js (without the imports):

const useStyles = makeStyles((theme) => ({
  root: {
    maxWidth: 345,
  },
  media: {
    height: 0,
    paddingTop: '56.25%', // 16:9
  },
}));

export default function Notecard( {notecards} ) {
  const classes = useStyles();
  const next = () => {
    console.log('Next Button Clicked')
  };
  const previous = () => {
    console.log('Back Button Clicked')
  };
  const hint = () => {
    console.log('Hint Button Clicked')
  };
  console.log({notecards});

  return (
    <Card className={classes.root}>
      <div id="cardBody">
      <CardHeader
        title="Kate Trivia"
        // subheader="Hint: In the 20th century"
      />
      <CardContent>
        <LoadQuestions notecards={notecards}/>
      </CardContent>
      </div>
    </Card>
  );
}
LoadQuestions.js (without imports)

const {useState} = React;

export default function LoadQuestions( {notecards} ) {
    const [currentIndex, setCounter] = useState(0);
    console.log({notecards});

    return (
    <div>
        <Toggle
            props={notecards}
            render={({ on, toggle }) => (
                <div onClick={toggle}>
                    {on ? 
                    <h1>{props.notecards} hi</h1> : 
                    <h1>{this.props[currentIndex].backSide}</h1>
                    }
                </div>
            )}
        />
        <button onClick={() => {
            console.log({notecards})
            if (currentIndex < (this.props.length-1)) {
                setCounter(currentIndex + 1);
            } else {
                alert('no more cards')
            }
            }}>Next Card
        </button>
        <button onClick={() => {
            if (currentIndex > 0 ) {
                setCounter(currentIndex -1);
            } else {
                alert('no previous cards')
            }
            }}>Previous Card
        </button>
    </div>
    );
}

Thanks in advance!

That's all the details I have for you, but stack overflow really wants me to add more before it will submit. Sorry!

question from:https://stackoverflow.com/questions/65925875/passing-property-to-another-component-react

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You should check if props exists, first time it renders the component it has no props so it shows undefined.

First i must say you destructured notecards out, so no need to use props.

If you want to use props you should change

({notecards}) to (props)

and if not you can directly use notecards since it is destructured

I suggest you two ways

adding question mark to check if exists

 <h1>{props?.notecards} hi</h1>//in the case you want to use props

or

add the props in a if statement

 <h1>{props.notecards?props.notecards:''} hi</h1> // if notecards is destructured remove the "props."

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...