javascript - 如何在 react 中使用钩子(Hook)传递 this.props.children

标签 javascript reactjs

这是我第一次创建 React 应用程序。

我想将按钮从 index.js 传递到使用 hooks 的表格组件。

Declaration:

  const testButton = () => {
    return (
      <React.Fragment>
        <button>Test1</button>
        <button>Test2</button>
        <button>Test3</button>
        <button>Test4</button>
        <button>Test5</button>
      </React.Fragment>
    );
  };

传递给Table组件

 return (
    <React.Fragment>
      <TestTable
        {...{
          testButton}} />
    </React.Fragment>

然后,表格组件将使用它来呈现包含按钮的表格。

export default function TestTable({
testButton,
  ...props
}) 

return (
{testButton});

我做得对吗?

如何从 index.js 中导出它,在 Table component.js 中导入,并在 Table 组件中渲染?

谢谢。

最佳答案

我想你想要的是:

const TestTable = ({ children }) => {
  return (
    <table>
      <tr>
        <td>Something...</td>
        <td>{children}</td>
      </tr>
    </table>
  )
}

然后:

const App = () => {
  return (
    <TestTable>
      <button>Test 1</button>
      <button>Test 2</button>
      <button>Test 3</button>
    </TestTable>
  )
}

希望对您有所帮助!

关于javascript - 如何在 react 中使用钩子(Hook)传递 this.props.children,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56272947/

相关文章:

javascript - Shiny :在非事件 tabPanel 上更新 DT

reactjs - 为什么我的 NavLink 没有使用 Semantic-ui-react 设置为事件状态?

javascript - react : access component function from another component

javascript - 使用 Javascript 移动图片

javascript - 弗里斯比中的访问 header

reactjs - React Router 传递参数。如何?

javascript - 声明一个全局谷歌地图对象

c - 如何让 Reactjs 和 MDL 协同工作?

javascript - 向下递归 DOM 树

javascript - 反向 jquery 倒数计时器