I know I can pass props
while rendering a component. I'm also aware of the getInitialState
method. But the problem is, getInitialState
isn't quite helping because my component doesn't know it's initial state. I do. So I want to pass it while I'm rendering it.
Something like this (pseudo-code):
React.render(<Component initialState={...} />);
I know I could use a prop
to work as the initial state but this smells like an anti-pattern.
What should I do?
EDIT FOR CLARITY
Imagine I have a CommentList
component. By the time I first render it, the initial state corresponds to the snapshot of current comments from my database. As the user includes comments, this list will change, and that's why it should be a state
and not props
. Now, in order to render the initial snapshot of comments I should pass it to the CommentsList
component, because it has no way to know it. My confusion is that the only way I see to pass this information is through a props
which seems to be an anti-pattern.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…