在一个页面上,我有一个嵌入HTML中的元素:

{dataTable}

初始化为:

  const [dataTable, setDataTable] = useState(<p>Data Table Loading...</p>);

并在useEffect函数中进行更新,我在其中调用数据库(supply PostgreSQL),以获取待办事项的数据.

let dataTable = <DataTable todos={todos} todoLogs={todoLogs} deleteTodo={deleteTodo} toggleTodo={toggleTodo}></DataTable>

setDataTable(dataTable)

但当它嵌入到页面中时,什么也不会显示,而且我也不会得到任何错误.

其他具有</h1>Hello World</h1>等siple struct 的元素确实会显示在页面上,并且当使用useState()等时,就像dataTable一样

当我console.log() dataTable元素时,我得到:

Browser Console console.log(dataTable) output

Object
$$typeof
: 
Symbol(react.element)
key
: 
null
props
: 
{todos: null, todoLogs: null, deleteTodo: ƒ, toggleTodo: ƒ}
ref
: 
null
type
: 
class DataTable
_owner
: 
null
_store
: 
{validated: true}
_self
: 
undefined
_source
: 
{fileName: 'C:/Users/user123/Desktop/coding-projects/2 }} testin…}/{{REACT}}/table-todo-testing/src/pages/Home.jsx', lineNumber: 132, columnNumber: 21}
[[Prototype]]
: 
Object

如果您感兴趣,以下是DataTable组件类的外观:

class DataTable extends React.Component {

    constructor(props) {
        super(props);

        this.todos = props.todos
        this.todoLogs = props.todoLogs
        this.deleteTodo = props.deleteTodo

      }


  render() {

    const data = this.todoLogs;
    

    // Map over the data to generate table rows

    try {

      const tableRows = data.map((todoLog) => (


        <tr key={todoLog.id}>
          <td>{this.todos.find(x => x.id === todoLog.id).title}</td>
          <td><input type="checkbox" onChange={() => {this.todos.find(x => x.id === todoLog.id)}} checked={todoLog.d09_04_24}></input></td>
          <td><input type="checkbox" onChange={() => {this.todos.find(x => x.id === todoLog.id)}} checked={todoLog.d08_04_24}></input></td>
          <td><input type="checkbox" onChange={() => {this.todos.find(x => x.id === todoLog.id)}} checked={todoLog.d07_04_24}></input></td>
          <td><input type="checkbox" onChange={() => {this.todos.find(x => x.id === todoLog.id)}} checked={todoLog.d06_04_24}></input></td>
          <td><input type="checkbox" onChange={() => {this.todos.find(x => x.id === todoLog.id)}} checked={todoLog.d05_04_24}></input></td>
          <td><input type="checkbox" onChange={() => {this.todos.find(x => x.id === todoLog.id)}} checked={todoLog.d04_04_24}></input></td>
          <td><input type="checkbox" onChange={() => {this.todos.find(x => x.id === todoLog.id)}} checked={todoLog.d03_04_24}></input></td>
          <td><input type="checkbox" onChange={() => {this.todos.find(x => x.id === todoLog.id)}} checked={todoLog.d02_04_24}></input></td>
          <td><button><i className="material-icons" onClick={() => {this.deleteTodo(todoLog.id)}}>delete</i></button></td>
        </tr>
      ));

      return (
        <table>
          <thead>
            <tr>
              <th>Title</th>
              <th>09/04</th>
              <th>08/04</th>
              <th>07/04</th>
              <th>06/04</th>
              <th>05/04</th>
              <th>04/04</th>
              <th>03/04</th>
              <th>02/04</th>
            </tr>
          </thead>
          <tbody>
            {tableRows}
          </tbody>
        </table>
      );
     

    } catch (error) {

      console.log("22 could not render table, data not available", error)

    }

    // Render the table with the generated rows
    
  }
}

谢谢

我try 过:

  • 研究问题

  • try 嵌入其他html元素并获得结果

  • 确保代码的其他部分无错误

推荐答案

看来您的DataTable组件无效.

try 删除渲染组件中的try/catch块,以确保返回某些内容(catch块中的情况并非如此).

class DataTable extends React.Component {
  render() {
    return <p>Table</p>
  }
}

Javascript相关问答推荐

如何将剧作家请求对象转换为字符串,因为它只提供promise ?

我无法使用tailwind-css和reactJS修改图像的位置

如何在ReactJS中修复这种不需要的按钮行为?

如何比较嵌套对象的更改并创建更改报告

如何在alpinejs中显示dev中x-for的元素

如何在JavaScript中在文本内容中添加新行

未捕获错误:在注销后重定向到/login页面时找不到匹配的路由

在vercel throws上部署带有gunjs的sveltekit应用无法找到模块./' lib/文本编码'

在nextjs服务器端api调用中传递认证凭证

Chart.js 4.4.2,当悬停在一个数据点上时,如何在工具提示中拥有多个数据点/标签?

阿波罗返回的数据错误,但在网络判断器中是正确的

在286之后恢复轮询

我可以使用使用node.js创建的本地主机来存储我网站上提交的所有数据吗?没有SQL或任何数据库.只有HTML语言

Web Crypto API解密失败,RSA-OAEP

如何在不影响隐式类型的情况下将类型分配给对象?

变量在导入到Vite中的另一个js文件时成为常量.

expo 联系人:如果联系人的状态被拒绝,则请求访问联系人的权限

对具有相似属性的对象数组进行分组,并使用串连的值获得结果

是否可以将异步调用与useState(UnctionName)一起使用

为什么在运行于<;img>;事件处理程序中的JavaScript中x和y是不可变的?