首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >有条件地渲染一个div

有条件地渲染一个div
EN

Stack Overflow用户
提问于 2022-04-09 16:13:13
回答 2查看 55关注 0票数 1

我试图添加一个提示选项到我的小测验应用程序。如果单击了50/50按钮,我希望呈现newAnswers数组。否则,我希望呈现shuffledAnswers数组。

当我运行这段代码时

TypeError:无法读取null属性(读取'useState')

我在这里做错什么了?

代码语言:javascript
复制
import React from "react";
import { useState } from "react/cjs/react.production.min";
import Card from "../UI/Card";
import "./DisplayQuestion.css";
import ProgressBar from "./Progress";

const DisplayQuestion = (props) => {
  /*I used dangerouslySetInnerHTML to fix the quotes gibberish problem */
  const [hint, setHint] = useState(false);
  console.log("hint: ", hint);
  let notBoolean = false;
  const newAnswers = [
    props.data.correct_answer,
    props.data.incorrect_answers[0],
  ];

  /*Helper functions */

  // shuffles the answers
  let shuffledAnswers = [
    props.data.correct_answer,
    ...props.data.incorrect_answers,
  ].sort(() => Math.random() - 0.5);

  if (shuffledAnswers.length > 2) {
    notBoolean = true;
    console.log("notBoolean");
  }
  const answersHintHandler = () => {
    setHint(true);
    console.log("hint: ", hint);
    console.log(newAnswers);
  };

  let progress = Math.round((props.score / props.numOfQuestions) * 100);

  return (
    <div>
      <h3 class="diff">Difficulty: {props.diff}</h3>
      <div classname="Questions">
        <Card>
          <ProgressBar bgcolor="#99ccff" progress={progress} height={30} />
          <h2>
            Questions {props.index}/{props.numOfQuestions}
          </h2>

          <h2
            className="question-text"
            dangerouslySetInnerHTML={{
              __html: props.data.question,
            }}
          />

          <ul>
            {notBoolean ? (
              <button onClick={answersHintHandler}>50/50</button>
            ) : (
              <p></p>
            )}
            {hint
              ? newAnswers.map((answer) => {
                  return (
                    <li
                      onClick={() => props.handler(answer)}
                      dangerouslySetInnerHTML={{
                        __html: answer,
                      }}
                    ></li>
                  );
                })
              : shuffledAnswers.map((answer) => {
                  return (
                    <li
                      onClick={() => props.handler(answer)}
                      dangerouslySetInnerHTML={{
                        __html: answer,
                      }}
                    ></li>
                  );
                })}
          </ul>
        </Card>
      </div>
    </div>
  );
};

出口违约DisplayQuestion;

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2022-04-09 16:45:49

从错误的路径x导入useState )

代码语言:javascript
复制
import { useState } from "react/cjs/react.production.min";

关照自动进口哈哈

票数 0
EN

Stack Overflow用户

发布于 2022-04-09 16:58:56

将第1行和第2行改为

代码语言:javascript
复制
import React, { useState } from 'react'
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71809778

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档