I will try and explain best I can.
I want to create a Carousel dynamically.
This is the representation of my data going into making this happen.
Below is a question array. This is represented by a question with (in this case ) four answers to choose from in the question_list_items.
let question = [{ id: 52, question: "I still enjoy the things I used to", type: "Text list",
question_list_items: Array(4)
0: {id: 164, list_item: "Definitely as much", item_value: "0", sort_order: 4, question_id: 52, …}
1: {id: 165, list_item: "Not quite so much", item_value: "1", sort_order: 3, question_id: 52, …}
2: {id: 166, list_item: "Only a little", item_value: "2", sort_order: 2, question_id: 52, …}
3: {id: 167, list_item: "Not at all", item_value: "3", sort_order: 1, question_id: 52, …}
];
Main render
Here is the start of the Carousel component. I start with the header section and call the <Questionnaire/> component to fill in the body.
return (
<Carousel
activeIndex={index}
direction={direction}
onSelect={(i,e)=>this.handleSelect(i,e)}
>
<Questionnaire/>
</Carousel>
);
Questionnaire Component
It is here where I think it goes wrong.
Here I loop over all the questions inside this.props.questions.
I check the Type of the question to render the correct type question.
This is done with the help of a switch. In this case Im only showing the <TextList /> component.
const Questionnaire = SortableContainer(({items}) => {
this.props.questions.map((row, index) => {
let element;
switch (row.type) {
case "Text list":
element = <TextList key={`questionList-${index}`} index={index} row={row} />;
break;
}
return element;
})
});
TextList Component
In this component is where I build a single slide of the carousel.
This component Calls the <TextListButton /> component to render the answer options for the current question.
const TextList = SortableElement(({row}) => {
console.log("row",row);
return (
// ******************************************************************************************************
// ************************************ LIST TYPE QUESTION *************************************
// ******************************************************************************************************
<Carousel.Item style={{"padding": "0 20px 0 20px", height: "600px", width: "1024px", textAlign: "center"}}>
<h3>{row.question}</h3>
<div style={{ paddingTop: "40px" }}>
<Row>
<Col xs={3} />
<Col xs={6}>
<div>
<ButtonGroup vertical block>
{row.question_list_items.map((row, idx) => {
return <TextListButton key={`buttonList-${idx}`} index={idx} row={row} />;
})}
</ButtonGroup>
</div>
</Col>
<Col xs={3} />
</Row>
</div>
<Carousel.Caption>
<p>Nulla vitae elit libero, a pharetra augue mollis interdum.</p>
</Carousel.Caption>
</Carousel.Item>
);
});
TextListButton Component This creates each answer item to choose from.
const TextListButton = SortableElement(({row}) => {
console.log("item row",row);
return (
<Button>{row.list_item}</Button>
);
});
NOTE on Questionnaire Component
I have tried to wrap the Questionnaire component in <div> tags. This makes it render, but its not a carousel anymore, its just one long list of questions and answers. Im not sure how to approach this. Any help will be appreciated.
const Questionnaire = SortableContainer(({items}) => {
return (
<div> <=********************************************HERE
{this.props.questions.map((row, index) => {
let element;
switch (row.type) {
case "Text list":
element = <TextList key={`questionList-${index}`} index={index} row={row} />;
break;
}
return element;
})}
</div> <=********************************************HERE
);
});
Update on declaring let element = null;
It seems to run for 14 questions, but then fails. ( there is only 14 questions )
STACT TRACE


let element = null;. If your switch dont match the defined case then you are returningundefinedthus the error.