我對寫我的表示/容器組件的方式感到非常困惑/不確定。尤其是在嵌套多種方式時。我很想談談如何更好地分離這些問題。嵌套表示/容器組件和可重用性
以下是我的(相關)redux狀態時合併(非常簡化)。
{
questions: [ // Need to get fetched from redux via selector
{
id: 1,
questionAnswers: [ // Need to get fetched from redux via selector
{
id: 1,
question: 'some question',
amount: 10 // Need to get fetched from redux via selector
},
{
id: 2,
question: 'some question',
amount: 10 // Need to get fetched from redux via selector
}
]
},
{
id: 2,
questionAnswers: [ // Need to get fetched from redux via selector
{
id: 3,
question: 'some question',
amount: 10 // Need to get fetched from redux via selector
},
{
id: 4,
question: 'some question',
amount: 10 // Need to get fetched from redux via selector
}
]
},
]
}
因此,目前我創建了以下組件來呈現這與正確的數據,它工作正常。
網頁組件
const Page1 =() => ({
<QuestionList />
});
QuestionList(容器)
// Gets all questions from the store
const QuestionList = ({ questions }) => ({
{questions.map(question => <Question question={question} />)}
});
問題(容器)
// Gets all questionAnswers for a question from the store
const Question = ({ question, questionAnswers }) => ({
<div>
<h3>The question itself</h3>
<div>{questionAnswers.map(answer => <QuestionAnswer answer={answer} />)}</div>
</div>
});
問題答案(容器)
// Gets the amount for each QuestionAnswer from the store
const QuestionAnswer = ({ answer, amount }) => ({
<div>
<div>{answer}</div>
<div>{amount}</div>
</div>
});
問題是當我要重複使用的questionList
組分與從存儲不同的數據。我將如何最好地解決這個問題?我是否應該將所有數據連接到頂層QuestionList
而不是QuestionAnswer
並使所有子組件都是表示性的?這感覺很奇怪,因爲它需要包含所有的questions
,questionAnswers
和questionAnswer amounts
。
謝謝!但是,如何處理在底層組件中提取的狀態,也就是'QuestionAnswer':/我想我需要重寫整個事情,因爲它現在變得非常混亂。 – NealVDV