Coffeescript - React 回调:发送 child header 单击返回 parent
Coffeescript - React callback: sending child header click back to parent
刚开始学习 Coffeescript,我正在使用 React.js。我试图确定点击了哪个,有人建议我不要在每个 header 上使用 data-attributes。我有一些想法如何在 handleHeaderClick 函数下处理这个问题,但我不确定它们应该如何实现。我也在考虑将 ContactsTable 组件拆分为 ContactsTableHeader 组件和 ContactsTableRow 组件,但我应该在 ContactsTableHeader 中仍然遇到同样的问题 - 确定单击了哪个 header。
#Application.cjsx
handleHeaderClick: ->
# childComponent.props
# childComponent.refs
# React.findDOMNode(childComponent.refs.firstName)
# React.findDOMNode(childComponent.refs.lastName)
# React.findDOMNode(childComponent.refs.age)
render: ->
<div>
<ContactsTable contactList={@state.contacts} onClick={@handleHeaderClick} />
</div>
#ContactsTable.cjsx
render: ->
if @props.contactList
contactsList = @props.contactList.map (contact) ->
<tr><td>{"#{contact.firstName}"}</td><td>{"#{contact.lastName}"}</td><td>{contact.age}</td></tr>
<table style={tableStyle}>
<thead style={headerStyle} onClick=@props.onClick>
<tr>
<th>FirstName</th>
<th>Last Name</th>
<th>Age</th>
</tr>
</thead>
<tbody>
{contactsList}
</tbody>
</table>
你可以这样做。
#Application.cjsx
handleHeaderClick: (header) ->
@setState({clickedHeader: header})
#do something else
render: ->
<div>
<ContactsTable contactList={@state.contacts} onClick={@handleHeaderClick} />
</div>
#ContactsTable.cjsx
render: ->
if @props.contactList
contactsList = @props.contactList.map (contact) ->
<tr><td>{"#{contact.firstName}"}</td><td>{"#{contact.lastName}"}</td><td>{contact.age}</td></tr>
<table style={tableStyle}>
<thead style={headerStyle}>
<tr>
<th onClick={@props.onClick('FirstName')}>FirstName</th>
<th onClick={@props.onClick('LastName')}>Last Name</th>
<th onClick={@props.onClick('Age')}>Age</th>
</tr>
</thead>
<tbody>
{contactsList}
</tbody>
</table>
此处 clickedHeader 保留在 Application.cjsx 中存在的组件内。您也可以将其保存在 ContactsTableHeader
中,看起来应该与此类似。
刚开始学习 Coffeescript,我正在使用 React.js。我试图确定点击了哪个,有人建议我不要在每个 header 上使用 data-attributes。我有一些想法如何在 handleHeaderClick 函数下处理这个问题,但我不确定它们应该如何实现。我也在考虑将 ContactsTable 组件拆分为 ContactsTableHeader 组件和 ContactsTableRow 组件,但我应该在 ContactsTableHeader 中仍然遇到同样的问题 - 确定单击了哪个 header。
#Application.cjsx
handleHeaderClick: ->
# childComponent.props
# childComponent.refs
# React.findDOMNode(childComponent.refs.firstName)
# React.findDOMNode(childComponent.refs.lastName)
# React.findDOMNode(childComponent.refs.age)
render: ->
<div>
<ContactsTable contactList={@state.contacts} onClick={@handleHeaderClick} />
</div>
#ContactsTable.cjsx
render: ->
if @props.contactList
contactsList = @props.contactList.map (contact) ->
<tr><td>{"#{contact.firstName}"}</td><td>{"#{contact.lastName}"}</td><td>{contact.age}</td></tr>
<table style={tableStyle}>
<thead style={headerStyle} onClick=@props.onClick>
<tr>
<th>FirstName</th>
<th>Last Name</th>
<th>Age</th>
</tr>
</thead>
<tbody>
{contactsList}
</tbody>
</table>
你可以这样做。
#Application.cjsx
handleHeaderClick: (header) ->
@setState({clickedHeader: header})
#do something else
render: ->
<div>
<ContactsTable contactList={@state.contacts} onClick={@handleHeaderClick} />
</div>
#ContactsTable.cjsx
render: ->
if @props.contactList
contactsList = @props.contactList.map (contact) ->
<tr><td>{"#{contact.firstName}"}</td><td>{"#{contact.lastName}"}</td><td>{contact.age}</td></tr>
<table style={tableStyle}>
<thead style={headerStyle}>
<tr>
<th onClick={@props.onClick('FirstName')}>FirstName</th>
<th onClick={@props.onClick('LastName')}>Last Name</th>
<th onClick={@props.onClick('Age')}>Age</th>
</tr>
</thead>
<tbody>
{contactsList}
</tbody>
</table>
此处 clickedHeader 保留在 Application.cjsx 中存在的组件内。您也可以将其保存在 ContactsTableHeader
中,看起来应该与此类似。