簡體   English   中英

如何修復不能作為子<a>警告出現?</a>

[英]How to fix <td> cannot appear as a child of <a> warning?

錯誤 如何修復這些警告? 我使用了材質 ui 表格組件。 我認為警告來自component={Link} to={ /patient/${patient.id} }

<TableContainer className={styles.tableContainer} component={Paper}>
      <Table aria-label="patients infomation table">
        <TableHead className={styles.tableHead}>
          <TableRow>
            <TableCell align="right">First Name</TableCell>
            <TableCell align="right">Last name</TableCell>
            <TableCell align="right">DOB</TableCell>
            <TableCell align="right">Phone Number</TableCell>
            <TableCell align="right">Email</TableCell>
          </TableRow>
        </TableHead>
        <TableBody>
          {patients.map((patient) => (
            <TableRow
              component={Link}
              to={`/patient/${patient.id}`}
              onClick={selectPatientClick}
              key={patient.id}
              className={styles.tableRow}
            >
              <>
                <TableCell align="right">{patient.fName}</TableCell>
                <TableCell align="right">{patient.lName}</TableCell>
                <TableCell align="right">{patient.dob}</TableCell>
                <TableCell align="right">{patient.pNumber}</TableCell>
                <TableCell align="right">{patient.email}</TableCell>
              </>
            </TableRow>
          ))}
        </TableBody>
      </Table>
    </TableContainer>

原因是您使用 Link 作為 TableRow 的組件。 prop 組件告訴 Material-UI 組件基於它將呈現所選組件的內容。 不要將鏈接作為組件傳遞,而是使用 TableRow 上的hover ...

 {patients.map((patient) => ( <TableRow hover onClick={selectPatientClick} key={patient.id} className={styles.tableRow} > <> <TableCell align="right">{patient.fName}</TableCell> <TableCell align="right">{patient.lName}</TableCell> <TableCell align="right">{patient.dob}</TableCell> <TableCell align="right">{patient.pNumber}</TableCell> <TableCell align="right">{patient.email}</TableCell> </> </TableRow> ))}

並在 function 中傳遞給該組件的 onClick 處理程序,使用history.push('/desired-route')以編程方式導航到您需要的路線。 只需從 react-router-dom 導入 useHistory 鈎子

 import {useHistory} from 'react-router-dom'... const history = useHistory() const selectPatientClick = () => { //do whatever you need and finally... history.push(`/patient/${patient.id}`) }

您不能使用 Link 作為 TableRow 的組件,而是使用useHistoryuseRouteMatch鏈接到您想要的頁面,

import {useHistory, useRouteMatch} from 'react-router-dom'
...
const {url} = useRouteMatch()
const history = useHistory()
...
<TableRow
  onClick={() => history.push(`${url}/${patient.id}`)}
  key={patient.id}
  className={styles.tableRow}
>
...

如何修復警告:validateDOMNesting(...):<div> 不能作為孩子出現</div><div id="text_translate"><p>我將用戶列表作為道具傳遞給 UserItem 組件,以迭代用戶列表並將它們顯示在表上。 列表顯示正確,我的渲染返回中沒有任何 div,但我仍然收到錯誤:index.js:1446 Warning: validateDOMNesting(...): cannot appear as a child of.</p><p> 嘗試了網上找到的許多解決方案,但沒有一個有效</p><p>用戶管理代碼:</p><pre> import React, { Component } from 'react'; import PropTypes from 'prop-types'; import { connect } from 'react-redux'; import Spinner from './common/Spinner'; import { getUsers } from '../actions/userActions'; import UserItem from './UserItem'; class UsersManagement extends Component { componentDidMount() { if (.this.props.auth.isAuthenticated) { this.props.history;push('/login'). } this.props;getUsers(), } render() { const { users. loading } = this.props;user; let usersList. if (users === null || loading) { usersList = <Spinner /> } else { if (users.length > 0) { usersList = users.map(user => ( <UserItem key={user._id} user={user} /> )) } else { usersList = <h2>No users</h2> } } return ( <div className="row"> <div className="col-12"> <h1 className="text-center mb-2">Users Management</h1> <button type="button" className="btn btn-success mb-4">New User</button> <table className="table"> <thead> <tr> <th scope="col">Options</th> <th scope="col">Username</th> <th scope="col">Email</th> <th scope="col">Phone Number</th> </tr> </thead> <tbody> {usersList} </tbody> </table> </div> </div> ) } } UsersManagement:propTypes = { getUsers. PropTypes.func,isRequired: auth. PropTypes.object,isRequired: user. PropTypes.object:isRequired } const mapStateToProps = state => ({ auth. state,auth: user. state,user }) export default connect(mapStateToProps; { getUsers })(UsersManagement);</pre><p> 用戶項目代碼:</p><pre> import React, { Component } from 'react'; import PropTypes from 'prop-types'; class UserItem extends Component { render() { const { user } = this.props; console.log(user); return ( <tr> <th scope="row"> <button type="button" className="btn btn-primary fa-xs mr-1"><i className="fas fa-pencil-alt"></i></button> <button type="button" className="btn btn-danger fa-xs"><i className="far fa-trash-alt"></i></button> </th> <td>{user.username}</td> <td>{user.email}</td> <td>{user.phone}</td> </tr> ) } } UserItem.propTypes = { user: PropTypes.object.isRequired } export default UserItem;</pre><p> 我希望修復警告信息</p></div>

[英]How to fix Warning: validateDOMNesting(...): <div> cannot appear as a child of <tbody>

如何修復警告:validateDOMNesting(...):不能作為子項出現<div> . 在身體在 Header</div><div id="text_translate"><p> 嘗試了網上找到的許多解決方案,但沒有一個有效,我的身體實際上不在 <div> 中,已經嘗試將 <Header /> 放入 <React.Fragment> 中也無濟於事。 老實說,不要搞錯</p><p>我的索引.js</p><pre> import React from "react"; import ReactDOM from "react-dom"; import "./index.css"; import Header from "./Header"; ReactDOM.render( <React.StrictMode> <Header /> </React.StrictMode>, document.getElementById("root"), );</pre><p> 我的 Header.js</p><pre> function Header() { return ( <body className="leading-none "> <header> <section className="bg-black"> <div className="m-auto flex-wrap py-3 flex justify-between max-w-6xl header-top-container px-3.5 text-neutral-50"> <div className="header-top-left"> <span className="text-xs font-normal inline-block h-4 pt-1 pr-6 mr-5 pl-5 "> Магазин в центре Киева </span> <span className="text-xs font-normal inline-block h-4 pt-1 pr-6 mr-5 pl-5 pl-4 "> <a href="tel:123456">+38 067 314 16 11</a> </span> <span className="text-xs font-normal inline-block h-4 pt-1 pr-6 mr-5 pl-6 "> Поки що приймаємо лише онлайн замовлення. </span> </div> <div className="header-top-right relative self-start "></div> <div className="text-xs header-btn-login pr-6 mr-5 inline-block align-top"> <a href="lgn">Вход</a> </div> </div> </section> <section className="bg-white container-white uppercase"> <div className="flex justify-between "> <img width="95px" height="55px" src="snkr.jpg" alt="logo" /> <h6>Бренды</h6> <h6>Новинки</h6> <h6>Обувь</h6> <h6>Одежда</h6> <h6>Скейтбординг</h6> <div className="headerCartBlock pr-1 float-right"> <div className=" inline-block mr-5 pt-0.5 align-top tracking-wide"> <a href="/">Избранное</a> </div> <div className="inline-block align-top tracking-wide"> <a href="/">Корзина</a> </div> </div> </div> </section> </header> </body> ); } export default Header;</pre><p> 我希望修復警告信息</p></div>

[英]How to fix Warning:validateDOMNesting(...): <body> cannot appear as a child of <div>. at body at Header

Gatsby 警告:validateDOMNesting(...):不能作為子項出現<div></div><div id="text_translate"><p>我正在學習通過幾個教程構建 Gatsby 站點。 我正在使用 Gatsby 2.13.50(CLI:2.7.14)。 通常,這些教程會教我們構建基本模板。 當我打開開發工具到控制台時,它加載得很好,除了這個警告:</p><pre> Warning: validateDOMNesting(...): <html> cannot appear as a child of <div>.</pre><p> 警告非常非常長。 我將在我的代碼之后發布它的 rest。</p><p> 我的 layout.js 基本模板位於 /src/components/layout.js。 它看起來像這樣:</p><pre> import React from "react" const Layout = (props) => { return ( <html lang="en"> <head> <Helmet> <meta charSet="utf-8" /> <title>Demo</title> </Helmet> </head> <body> {props.children} </body> </html> ) } export default Layout</pre><p> 它由 /src/pages/index.js 使用,如下所示:</p><pre> import React from 'react' import Layout from '../components/layout' const IndexPage = () => { return ( <Layout> <h1>Demo onle.</h1> <p>Hello. This totally works fine.</p> </Layout> ) } export default IndexPage</pre><p> 如前所述,它有效,除了開發工具控制台中的非常長的警告。</p><p> 我用谷歌搜索了這個,但還沒有發現這是怎么發生的以及如何避免它。 我發現有一個<a href="https://www.gatsbyjs.org/docs/custom-html/" rel="nofollow noreferrer">html.js</a>但我不知道這是否是某種默認模板以及我是否應該覆蓋它以用作基本模板。 我試過了,但性能很差,所以我認為我錯了。</p><p> 當我刪除<html>標簽時,我得到: Parsing error: Adjacent JSX elements must be wrapped in an enclosing tag.</p><p> 如果我用<div>替換它 它說<body> cannot appear as a child of <div></p><p> 這是很長的警告:</p><pre> Warning: validateDOMNesting(...): <html> cannot appear as a child of <div>. in html (at layout.js:10) in Layout (at chat.js:6) in ChatPage (created by HotExportedChatPage) in AppContainer (created by HotExportedChatPage) in HotExportedChatPage (created by PageRenderer) in PageRenderer (at json-store.js:93) in JSONStore (at root.js:51) in RouteHandler (at root.js:73) in div (created by FocusHandlerImpl) in FocusHandlerImpl (created by Context.Consumer) in FocusHandler (created by RouterImpl) in RouterImpl (created by Context.Consumer) in Location (created by Context.Consumer) in Router (created by EnsureResources) in ScrollContext (at root.js:64) in RouteUpdates (at root.js:63) in EnsureResources (at root.js:61) in LocationHandler (at root.js:119) in LocationProvider (created by Context.Consumer) in Location (at root.js:118) in Root (at root.js:127) in _default (at app.js:65) __stack_frame_overlay_proxy_console__ @ index.js:2177 warningWithoutStack @ react-dom.development.js:507 validateDOMNesting @ react-dom.development.js:8625 createInstance @ react-dom.development.js:8737 completeWork @ react-dom.development.js:16901 completeUnitOfWork @ react-dom.development.js:19143 performUnitOfWork @ react-dom.development.js:19341 workLoop @ react-dom.development.js:19353 renderRoot @ react-dom.development.js:19436 performWorkOnRoot @ react-dom.development.js:20343 performWork @ react-dom.development.js:20255 performSyncWork @ react-dom.development.js:20229 requestWork @ react-dom.development.js:20098 scheduleWork @ react-dom.development.js:19912 enqueueSetState @ react-dom.development.js:11170./node_modules/react/cjs/react.development.js.Component.setState @ react.development.js:335 (anonymous) @ index.js:104 requestAnimationFrame (async) (anonymous) @ index.js:102 Promise.then (async) (anonymous) @ index.js:100 (anonymous) @ history.js:70 navigate @ history.js:69 (anonymous) @ navigation.js:103 Promise.then (async) navigate @ navigation.js:77 window.___navigate @ navigation.js:150 navigate @ index.js:213 onClick @ index.js:184 onClick @ index.js:464 callCallback @ react-dom.development.js:150 invokeGuardedCallbackDev @ react-dom.development.js:200 invokeGuardedCallback @ react-dom.development.js:257 invokeGuardedCallbackAndCatchFirstError @ react-dom.development.js:271 executeDispatch @ react-dom.development.js:562 executeDispatchesInOrder @ react-dom.development.js:584 executeDispatchesAndRelease @ react-dom.development.js:681 executeDispatchesAndReleaseTopLevel @ react-dom.development.js:689 forEachAccumulated @ react-dom.development.js:663 runEventsInBatch @ react-dom.development.js:817 runExtractedEventsInBatch @ react-dom.development.js:825 handleTopLevel @ react-dom.development.js:4827 batchedUpdates$1 @ react-dom.development.js:20440 batchedUpdates @ react-dom.development.js:2152 dispatchEvent @ react-dom.development.js:4906 (anonymous) @ react-dom.development.js:20491 unstable_runWithPriority @ scheduler.development.js:255 interactiveUpdates$1 @ react-dom.development.js:20490 interactiveUpdates @ react-dom.development.js:2171 dispatchInteractiveEvent @ react-dom.development.js:4883</pre><p> 我應該如何修復此警告?</p></div>

[英]Gatsby Warning: validateDOMNesting(...): <html> cannot appear as a child of <div>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

相關問題 如何修復警告:validateDOMNesting(...):<div> 不能作為孩子出現</div><div id="text_translate"><p>我將用戶列表作為道具傳遞給 UserItem 組件,以迭代用戶列表並將它們顯示在表上。 列表顯示正確,我的渲染返回中沒有任何 div,但我仍然收到錯誤:index.js:1446 Warning: validateDOMNesting(...): cannot appear as a child of.</p><p> 嘗試了網上找到的許多解決方案,但沒有一個有效</p><p>用戶管理代碼:</p><pre> import React, { Component } from 'react'; import PropTypes from 'prop-types'; import { connect } from 'react-redux'; import Spinner from './common/Spinner'; import { getUsers } from '../actions/userActions'; import UserItem from './UserItem'; class UsersManagement extends Component { componentDidMount() { if (.this.props.auth.isAuthenticated) { this.props.history;push('/login'). } this.props;getUsers(), } render() { const { users. loading } = this.props;user; let usersList. if (users === null || loading) { usersList = <Spinner /> } else { if (users.length > 0) { usersList = users.map(user => ( <UserItem key={user._id} user={user} /> )) } else { usersList = <h2>No users</h2> } } return ( <div className="row"> <div className="col-12"> <h1 className="text-center mb-2">Users Management</h1> <button type="button" className="btn btn-success mb-4">New User</button> <table className="table"> <thead> <tr> <th scope="col">Options</th> <th scope="col">Username</th> <th scope="col">Email</th> <th scope="col">Phone Number</th> </tr> </thead> <tbody> {usersList} </tbody> </table> </div> </div> ) } } UsersManagement:propTypes = { getUsers. PropTypes.func,isRequired: auth. PropTypes.object,isRequired: user. PropTypes.object:isRequired } const mapStateToProps = state => ({ auth. state,auth: user. state,user }) export default connect(mapStateToProps; { getUsers })(UsersManagement);</pre><p> 用戶項目代碼:</p><pre> import React, { Component } from 'react'; import PropTypes from 'prop-types'; class UserItem extends Component { render() { const { user } = this.props; console.log(user); return ( <tr> <th scope="row"> <button type="button" className="btn btn-primary fa-xs mr-1"><i className="fas fa-pencil-alt"></i></button> <button type="button" className="btn btn-danger fa-xs"><i className="far fa-trash-alt"></i></button> </th> <td>{user.username}</td> <td>{user.email}</td> <td>{user.phone}</td> </tr> ) } } UserItem.propTypes = { user: PropTypes.object.isRequired } export default UserItem;</pre><p> 我希望修復警告信息</p></div> 如何修復 validateDOMNesting(...): 不能作為子級出現。 並且列表中的每個孩子都應該有一個唯一的“關鍵”道具 不能作為“div”的孩子出現? 如何修復警告:validateDOMNesting(...):不能作為子項出現<div> . 在身體在 Header</div><div id="text_translate"><p> 嘗試了網上找到的許多解決方案,但沒有一個有效,我的身體實際上不在 <div> 中,已經嘗試將 <Header /> 放入 <React.Fragment> 中也無濟於事。 老實說,不要搞錯</p><p>我的索引.js</p><pre> import React from "react"; import ReactDOM from "react-dom"; import "./index.css"; import Header from "./Header"; ReactDOM.render( <React.StrictMode> <Header /> </React.StrictMode>, document.getElementById("root"), );</pre><p> 我的 Header.js</p><pre> function Header() { return ( <body className="leading-none "> <header> <section className="bg-black"> <div className="m-auto flex-wrap py-3 flex justify-between max-w-6xl header-top-container px-3.5 text-neutral-50"> <div className="header-top-left"> <span className="text-xs font-normal inline-block h-4 pt-1 pr-6 mr-5 pl-5 "> Магазин в центре Киева </span> <span className="text-xs font-normal inline-block h-4 pt-1 pr-6 mr-5 pl-5 pl-4 "> <a href="tel:123456">+38 067 314 16 11</a> </span> <span className="text-xs font-normal inline-block h-4 pt-1 pr-6 mr-5 pl-6 "> Поки що приймаємо лише онлайн замовлення. </span> </div> <div className="header-top-right relative self-start "></div> <div className="text-xs header-btn-login pr-6 mr-5 inline-block align-top"> <a href="lgn">Вход</a> </div> </div> </section> <section className="bg-white container-white uppercase"> <div className="flex justify-between "> <img width="95px" height="55px" src="snkr.jpg" alt="logo" /> <h6>Бренды</h6> <h6>Новинки</h6> <h6>Обувь</h6> <h6>Одежда</h6> <h6>Скейтбординг</h6> <div className="headerCartBlock pr-1 float-right"> <div className=" inline-block mr-5 pt-0.5 align-top tracking-wide"> <a href="/">Избранное</a> </div> <div className="inline-block align-top tracking-wide"> <a href="/">Корзина</a> </div> </div> </div> </section> </header> </body> ); } export default Header;</pre><p> 我希望修復警告信息</p></div> 警告:validateDOMNesting(…):<div> 不能作為孩子出現<tbody> 反應<div>不能作為孩子出現<tr>警告 如何修復此警告 - 警告:validateDOMNesting(...): <a>cannot appear as a descendant of</a> <a>.</a> <a>下面是我的代碼</a> Gatsby 警告:validateDOMNesting(...):不能作為子項出現<div></div><div id="text_translate"><p>我正在學習通過幾個教程構建 Gatsby 站點。 我正在使用 Gatsby 2.13.50(CLI:2.7.14)。 通常,這些教程會教我們構建基本模板。 當我打開開發工具到控制台時,它加載得很好,除了這個警告:</p><pre> Warning: validateDOMNesting(...): <html> cannot appear as a child of <div>.</pre><p> 警告非常非常長。 我將在我的代碼之后發布它的 rest。</p><p> 我的 layout.js 基本模板位於 /src/components/layout.js。 它看起來像這樣:</p><pre> import React from "react" const Layout = (props) => { return ( <html lang="en"> <head> <Helmet> <meta charSet="utf-8" /> <title>Demo</title> </Helmet> </head> <body> {props.children} </body> </html> ) } export default Layout</pre><p> 它由 /src/pages/index.js 使用,如下所示:</p><pre> import React from 'react' import Layout from '../components/layout' const IndexPage = () => { return ( <Layout> <h1>Demo onle.</h1> <p>Hello. This totally works fine.</p> </Layout> ) } export default IndexPage</pre><p> 如前所述,它有效,除了開發工具控制台中的非常長的警告。</p><p> 我用谷歌搜索了這個,但還沒有發現這是怎么發生的以及如何避免它。 我發現有一個<a href="https://www.gatsbyjs.org/docs/custom-html/" rel="nofollow noreferrer">html.js</a>但我不知道這是否是某種默認模板以及我是否應該覆蓋它以用作基本模板。 我試過了,但性能很差,所以我認為我錯了。</p><p> 當我刪除<html>標簽時,我得到: Parsing error: Adjacent JSX elements must be wrapped in an enclosing tag.</p><p> 如果我用<div>替換它 它說<body> cannot appear as a child of <div></p><p> 這是很長的警告:</p><pre> Warning: validateDOMNesting(...): <html> cannot appear as a child of <div>. in html (at layout.js:10) in Layout (at chat.js:6) in ChatPage (created by HotExportedChatPage) in AppContainer (created by HotExportedChatPage) in HotExportedChatPage (created by PageRenderer) in PageRenderer (at json-store.js:93) in JSONStore (at root.js:51) in RouteHandler (at root.js:73) in div (created by FocusHandlerImpl) in FocusHandlerImpl (created by Context.Consumer) in FocusHandler (created by RouterImpl) in RouterImpl (created by Context.Consumer) in Location (created by Context.Consumer) in Router (created by EnsureResources) in ScrollContext (at root.js:64) in RouteUpdates (at root.js:63) in EnsureResources (at root.js:61) in LocationHandler (at root.js:119) in LocationProvider (created by Context.Consumer) in Location (at root.js:118) in Root (at root.js:127) in _default (at app.js:65) __stack_frame_overlay_proxy_console__ @ index.js:2177 warningWithoutStack @ react-dom.development.js:507 validateDOMNesting @ react-dom.development.js:8625 createInstance @ react-dom.development.js:8737 completeWork @ react-dom.development.js:16901 completeUnitOfWork @ react-dom.development.js:19143 performUnitOfWork @ react-dom.development.js:19341 workLoop @ react-dom.development.js:19353 renderRoot @ react-dom.development.js:19436 performWorkOnRoot @ react-dom.development.js:20343 performWork @ react-dom.development.js:20255 performSyncWork @ react-dom.development.js:20229 requestWork @ react-dom.development.js:20098 scheduleWork @ react-dom.development.js:19912 enqueueSetState @ react-dom.development.js:11170./node_modules/react/cjs/react.development.js.Component.setState @ react.development.js:335 (anonymous) @ index.js:104 requestAnimationFrame (async) (anonymous) @ index.js:102 Promise.then (async) (anonymous) @ index.js:100 (anonymous) @ history.js:70 navigate @ history.js:69 (anonymous) @ navigation.js:103 Promise.then (async) navigate @ navigation.js:77 window.___navigate @ navigation.js:150 navigate @ index.js:213 onClick @ index.js:184 onClick @ index.js:464 callCallback @ react-dom.development.js:150 invokeGuardedCallbackDev @ react-dom.development.js:200 invokeGuardedCallback @ react-dom.development.js:257 invokeGuardedCallbackAndCatchFirstError @ react-dom.development.js:271 executeDispatch @ react-dom.development.js:562 executeDispatchesInOrder @ react-dom.development.js:584 executeDispatchesAndRelease @ react-dom.development.js:681 executeDispatchesAndReleaseTopLevel @ react-dom.development.js:689 forEachAccumulated @ react-dom.development.js:663 runEventsInBatch @ react-dom.development.js:817 runExtractedEventsInBatch @ react-dom.development.js:825 handleTopLevel @ react-dom.development.js:4827 batchedUpdates$1 @ react-dom.development.js:20440 batchedUpdates @ react-dom.development.js:2152 dispatchEvent @ react-dom.development.js:4906 (anonymous) @ react-dom.development.js:20491 unstable_runWithPriority @ scheduler.development.js:255 interactiveUpdates$1 @ react-dom.development.js:20490 interactiveUpdates @ react-dom.development.js:2171 dispatchInteractiveEvent @ react-dom.development.js:4883</pre><p> 我應該如何修復此警告?</p></div> 反應:警告:validateDOMNesting(...):空白文本節點不能作為子節點出現 反應:警告:validateDOMNesting(…):文本節點不能顯示為以下子項<tbody>
 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM