简体   繁体   English

在 React 中,我如何知道哪个父组件渲染了子组件?

[英]In React, how do I know which parent component rendered a child component?

I'm making a table component that I want to be used across two pages, but I want one page to show two columns and the other page to not.我正在制作一个表格组件,我想在两页中使用它,但我希望一页显示两列,另一页不显示。 I'd like to make a gate inside the component that checks what the parent component is and changes it's render based on that.我想在组件内部创建一个门来检查父组件是什么,并根据它更改它的渲染。 Is this possible?这可能吗?

EX: of what I'd like the table to look like EX:我希望桌子看起来像什么

const EventTable = ({ events }) => (
    <Box>
      <TableBody>
          {events.map(
            ({
              amount,
              created,
              disappearingColumnA,
              id,
            }) => (
              <TableRow key={id}>
                <TableCell padding="small" className="created-cell">
                  <Small>{formatDateStandard(created)}</Small>
                </TableCell>
                <TableCell padding="small" className="amount-cell">
                  <Small>{centsToDollarsWithSign(amount)}</Small>
                </TableCell>

                {/* Relevant Code */}
                {!isChildOfPageA && (
                  <TableCell padding="small" className="disappearingColumnA-cell">
                    <Small>{centsToDollarsWithSign(disappearingColumnA)}</Small>
                  </TableCell>
                )}
              </TableRow>
            ),
          )}
        </TableBody>
    </Box>
  )

you can create functional component Table which will create columns and rows dynamicaly according to passed data.您可以创建功能组件表,它将根据传递的数据动态创建列和行。 But before passing data you should prepare it in parent component in a proper format.但是在传递数据之前,您应该以适当的格式在父组件中准备它。

here is simple example of one of my componente(based on Semantic-ui React):这是我的一个组件的简单示例(基于 Semantic-ui React):

return (
        <Table celled>
            <Table.Header>
                <Table.Row>
                    {
                        columnNames.map(columnName => {
                            return <Table.HeaderCell key={columnName}>
                                {columnName}
                            </Table.HeaderCell>
                        })
                    }
                </Table.Row>
            </Table.Header>

            <Table.Body>
                {
                    tableData.map((item) => {
                        return <Table.Row key={item.userId}>
                            <Table.Cell width={2}>{item.firstName}</Table.Cell>
                            <Table.Cell width={2}>{item.lastName}</Table.Cell>
                            <Table.Cell width={3}>{item.email}</Table.Cell>
                            <Table.Cell width={3}>{item.telephone}</Table.Cell>
                            

                            <Table.Cell>
                                <Button>
                                    Info
                                </Button>
                                
                                <Button>
                                    Delete
                                </Button>
                            </Table.Cell>
                        </Table.Row>
                    })
                }
            </Table.Body>
        </Table>

This table I needed in different pages.我在不同的页面中需要这张表。 So I divided it and pass separate column names and data.所以我把它分开并传递单独的列名和数据。 You can do similar thing - use such component in any page and pass from parent 2 data objects: 1 object\array with column names 2 object with data to fill table您可以做类似的事情 - 在任何页面中使用此类组件并从父 2 个数据对象传递:1 个对象\数组,列名 2 object,数据填充表

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在没有按钮 OnClick 事件的情况下,如何将存储在子组件中的 state 值传递给 React 中的父组件? - How do i get the state value which I have stored in the Child component to the Parent component in React without button OnClick events? 如何将多个事件侦听器从React中的父组件附加到子组件的同一事件中? - How do I attach multiple event listeners to the same event of a child component from its parent component in React? 如何在 React 中将数据从子组件发送到父组件 - How do I send data from child component to parent component in React 我如何在 React 中将事件从子组件发送到父组件? - How do I emit events from a child component to a parent component in React? 如何在子组件中从父组件重置道具? - How do I reset props from a parent component in a child component? 如何测试在其父级上调用函数的 React 组件,这会更改组件上的 props? - How do I test a React component that calls a function on its parent, which changes props on the component? 子组件未在父组件中正确呈现 - Child component is not rendered properly in the parent component 如何将子组件中的表单数据传递给父组件(也包含表单)并在提交日志时提交所有数据? - How do I pass my Form Data which is in the child component to the parent component(which also contains a form) and on submit log all the data? 在React.js的父组件中使用react-router时,如何使用react context API将数据从父组件传递到子组件? - How do I use react context API to pass data from parent component to child component when using react-router in the parent component in React.js? 在父组件中反应子组件 - React child Component in parent Component
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM