【发布时间】:2016-09-17 16:41:27
【问题描述】:
我注意到返回之前和返回组件后的数据之间存在差异。
class AComponent extends Component {
render() {
const body = <BComponent crmStatus={...}/>
debugger // log body on the right
// ... render as static html to electron window
return false
}
}
class BComponent extends Component {
render() {
const resultRender = <article className='large'>...</article>
debugger // log resultRender on the left
return resultRender
}
}
我以前的问题是“How to read rendered component's className?”,但我已经将问题拆分为回答 实际发生的事情以及为什么会这样真的开始困扰我,甚至可能给出我提示解决我的问题。
所以问题是:
组件实际发生了什么,为什么会这样?我的 render() 函数中可以有非常复杂的逻辑,但我想使用这些组件并不容易。
const headerContact = isContactInCRM ? <p>..</p> : <div>..</div>
const headerCallBtnsOrInfo = isSipEnabled && <div>..buttons..</div>
const callTimer = callDuration && <span>{callDuration}</span>
const footerNotes = <footer>..</footer>
const someImportedComponent = <MyComponent />
const resultRender = <section>
{headerContact}
{headerCallBtnsOrInfo}
{callTimer}
{footerNotes}
{someImportedComponent}
</section>
// there is a difference in data between headerContact and someImportedComponent
// when traversing the resultRender's tree in console
【问题讨论】:
-
我不确定这个问题是否仍然相关,但我在提供的数据中发现了一个主要问题: - 右侧屏幕截图是
BComponent组件存根的转储 - 左侧屏幕截图是article的转储组件存根...这就是它们具有不同类型的原因(注意类与字符串article)您可能想在第二种情况下转储“this”... -
组件返回后会形成一个VDOM(react会添加ids等)并追加到html中。但是,目前还不清楚您要解决什么问题!
-
@ZbigniewZagórski 感谢您的洞察力。这个问题已经很老了,在打开赏金之前我什至没有再读过它。你写的正是我当时的想法,但没看懂。可惜我不能奖励评论者。
标签: javascript reactjs components render