简体   繁体   English

由于返回数组的 function 的结果,我们如何在 useState 中设置 Initial state?

[英]How we can set Initial state in useState as a result of a function which returns an array?

my JSON object consist of correct_answer which is of string type and incorrect_answers which is an array I want to push correct answer in the incorrect_answers array,and after modification I want this JSON object as the initial value of our state modified that I have created using React hooks.How can I achieve this. my JSON object consist of correct_answer which is of string type and incorrect_answers which is an array I want to push correct answer in the incorrect_answers array,and after modification I want this JSON object as the initial value of our state modified that I have created using React钩子。我怎样才能做到这一点。

var css = "ma2 pa2 br-pill dim pointer grow hover w-20";

const Logic = (props) => {
  const ques = props.questions;
  const [count, setCount] = useState(0);
  //const [ques, setQues] = useState(props.questions);
  const [modified, setModified] = useState((ques) => () => {
    for (var i = 0; i < ques.length; i++) {
      ques[i].incorrect_answers.push(ques[i].correct_answer);
    }
    return ques;
  });
  return (
    <div className="tc">
      {modified.length !== 0 ? (
        <div>
          {modified[count].incorrect_answers.map((home) => (
            <div>
              <button className={css}>{home}</button>
            </div>
          ))}
        </div>
      ) : (
        <h1>loading</h1>
      )}
    </div>
  );
};

export default Logic;

my JSON object that i am getting as a props looks like我作为道具得到的 JSON object 看起来像

{
>     "response_code": 0,
>     "results": [
>         {
>             "category": "Entertainment: Video Games",
>             "type": "multiple",
>             "difficulty": "medium",
>             "question": "When was the original Star Wars: Battlefront II released?",
>             "correct_answer": "October 31, 2005",
>             "incorrect_answers": [
>                 "December 18, 2004",
>                 "November 21, 2006",
>                 "September 9, 2007"
>             ]
>         },
>         {
>             "category": "Entertainment: Japanese Anime & Manga",
>             "type": "multiple",
>             "difficulty": "medium",
>             "question": "In Dragon Ball Z, who was the first character to go Super Saiyan 2?",
>             "correct_answer": "Gohan",
>             "incorrect_answers": [
>                 "Goku",
>                 "Vegeta",
>                 "Trunks"
>             ]
>         },
>         {
>             "category": "Science & Nature",
>             "type": "multiple",
>             "difficulty": "easy",
>             "question": "The human heart has how many chambers?",
>             "correct_answer": "4",
>             "incorrect_answers": [
>                 "2",
>                 "6",
>                 "3"
>             ]
>         }
>          }

You can use useEffect hook to manupulate the data and store it in modified.您可以使用 useEffect 挂钩来操作数据并将其存储在修改后。

const [modified, setModified] = useState([]);    
    useEffect(() => {
           setModified(props.questions.map(data=>({...data,incorrect_answers:data.incorrect_answers.concat([data.correct_answer])})));
         }, [props.questions]);

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

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