React初心者です
定義してもundefinedエラーが出ています。
TypeError: Cannot read property 'question' of undefined App.displayNextQuestion src/App.jsx:23
App.jsx import React from 'react'; import defaultDataset from './dataset' import'./assets/styles/styles.css' import {AnswersList,Chats} from "./components/index" export default class App extends React.Component{ constructor(props){ super(props); this.state= { answers: [], chats: [], current: "init", dataset: defaultDataset, open: false, question: ["abc"] ここで定義? } this.selectAnswer = this.selectAnswer.bind(this) } displayNextQuestion = (nextQuestionId) => { エラーメソッド const chats = this.state.chats chats.push({ text: this.state.dataset[nextQuestionId].question, エラー部分 type: 'question', }) this.setState({ answers: this.state.dataset[nextQuestionId].answers, chats: chats, currentId: nextQuestionId }) } selectAnswer = (selectedAnswer, nextQuestionId) =>{ switch(true){ case(nextQuestionId === 'init'): setTimeout(() => this.displayNextQuestion(nextQuestionId), 500) エラーメソッド break; case(/https:*/.test(nextQuestionId)): const a = document.createElement('a'); a.href = nextQuestionId; a.target = '_blank'; a.click(); break; default: const chats = this.state.chats chats.push({ text: selectedAnswer, type: 'answer' }) this.setState ({ chats: chats }); setTimeout( () =>this.displayNextQuestion(nextQuestionId),700);エラーメソッド break; } } componentDidMount() { const initAnswer = "" this.selectAnswer(initAnswer, this.state.currentId) } componentDidUpdate() { const scrollArea = document.getElementById("scroll-area") if(scrollArea){ scrollArea.scrollTop = scrollArea.scrollHeight } } render(){ return ( <section className="c-section"> <div className="c-box"> <AnswersList answers={this.state.answers} select= {this.selectAnswer}/> <Chats chats={this.state.chats}/> </div> </section> ); } }
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2020/08/26 07:34
2020/08/26 07:57
2020/08/26 08:10
2020/08/26 08:16
2020/08/26 08:53