簡體   English   中英

TypeError:無法分配為只讀對象“#”的屬性“ exports” <Object> 在ReactJS中

[英]TypeError: Cannot assign to read only property 'exports' of object '#<Object>' in ReactJS

所以我想要實現的是從我的應用程序頁面中獲取一個變量,並能夠在其他頁面中使用它。 該變量是從導航欄上操作的,該導航欄可以從所需的每個頁面進行訪問。 “ App.js”基本上只是一個導航欄,其他頁面顯示在其下方。 其他頁面需要從導航欄中選擇/分配值,以便從數據庫中提取信息。

App.js

let year;
let term;

class App extends Component {

  render() {
    // Here I do things and assign the variables on top values
  }

export default App;

module.exports = year;
module.exports = term;

然后,在我的其中一頁上

var year = require("./AdminApp");
var term = require("./AdminApp");

class ViewStudents extends Component {
  constructor(props) {
    super(props);

    this.state = {
      year: year,
      term: term
    }
  }

  render() {
    // Do more magic
  }

export default ViewStudents;

這些是我的代碼的摘要版本,其中包括遇到問題的部分。 任何幫助表示贊賞! 提前致謝!

使用這樣的命名導出。

App.js

let year;
let term;

class App extends Component {

  render() {
    // Here I do things and assign the variables on top values
  }

export default App;
export { year, term };

在您的頁面中:

import { year, term } from "./AdminApp";

class ViewStudents extends Component {
  constructor(props) {
    super(props);

    this.state = {
      year: year,
      term: term
    }
  }

  render() {
    // Do more magic
  }

export default ViewStudents;

ReactJs:類型錯誤:無法分配給 object 的只讀屬性“cartHandler”'#<object> '?<div id="text_translate"><p> 在這里,我正在嘗試建立一個餐廳網站。 我已經構建了它的一半。 但我被困在某個時刻。 我收到錯誤。 我無法在我的代碼中找出問題所在。 每當我嘗試通過單擊添加按鈕添加新食物然后我得到 TypeError: Cannot assign to read only property 'cartHandler' of object '#'?.. 我試圖在 cartHandler object 中調試,但我得到了同樣的錯誤很多次。 有人可以檢查一下嗎?</p><p> <strong>這是我的 App.js 文件</strong></p><pre>import logo from './logo.svg'; import { BrowserRouter as Router, Switch, Route, Link } from "react-router-dom"; import './App.css'; import '../node_modules/bootstrap/dist/css/bootstrap.min.css' import Banner from './components/Banner/Banner'; import Header from './components/Header/Header'; import Foods from './components/Foods/Foods'; import Features from './components/Features/Features'; import Footer from './components/Footer/Footer'; // import SIgnUp from './components/SignUp/SIgnUp'; import NotFound from './components/NotFound/NotFound'; import FoodItemDetails from './components/FoodItemDetails/FoodItemDetails' import { createContext, useContext, useState } from 'react'; import Login from './components/Login/Login'; export const userContext = createContext(); function App() { const [cart, setCart] = useState([]); const [orderId, setOrderId] = useState(null); const [deliveryDetails, setDeliveryDetails] = useState({ todoor:null,road:null, flat:null, businessname:null, address: null }); const [userEmail, setUserEmail] = useState(null); const deliveryDetailsHandler = (data) =&gt; { setDeliveryDetails(data) } const getUserEmail = (email) =&gt; { setUserEmail(email); } const clearCart = () =&gt; { const orderedItems = cart.map(cartItem =&gt; { return {food_id: cartItem.id, quantity: cartItem.quantity} }) const orderDetailsData = { userEmail, orderedItems, deliveryDetails } fetch('https://red-onion-backend.herokuapp.com/submitorder', { method: "POST", headers: { "Content-type": "application/json" }, body: JSON.stringify(orderDetailsData) }).then(res =&gt; res.json()).then(data=&gt; setOrderId(data._id)) console.log(orderId); setCart([]) } const cartHandler = (data) =&gt; { const alreadyAdded = cart.find(crt =&gt; crt.id == data.id ); const newCart = [...cart,data] setCart(newCart); if(alreadyAdded){ const reamingCarts = cart.filter(crt =&gt; cart.id;= data); setCart(reamingCarts). }else{ const newCart = [..,cart;data] setCart(newCart), } } const checkOutItemHandler = (productId. productQuantity) =&gt; { const newCart = cart.map(item =&gt; { if(item.id == productId){ item;quantity = productQuantity; } return item. }) const filteredCart = newCart.filter(item =&gt; item,quantity &gt; 0) setCart(filteredCart) } const [logggedInUser; setLoggedInUser] = useState({}), const [signOutUser; setSignOutUser] = useState({}). return ( &lt;userContext,Provider value={([logggedInUser, setLoggedInUser], [signOutUser: setSignOutUser])}&gt; &lt;Router&gt; &lt;div className="App"&gt; &lt;Switch&gt; &lt;Route exact path="/"&gt; &lt;Header&gt;&lt;/Header&gt; &lt;Banner&gt;&lt;/Banner&gt; &lt;Foods&gt;&lt;/Foods&gt; &lt;Features&gt;&lt;/Features&gt; &lt;Footer&gt;&lt;/Footer&gt; &lt;/Route&gt; &lt;Route path="/user"&gt; &lt;Login&gt;&lt;/Login&gt; &lt;/Route&gt; &lt;Route path= "/food/.id"&gt; &lt;Header cart={cart}&gt;&lt;/Header&gt; {/* &lt;FoodItemDetails cart={cart} cartHandler={cartHandler}&gt;&lt;/FoodItemDetails&gt; */} &lt;FoodItemDetails cart={cart} cartHandler={cartHandler}&gt;&lt;/FoodItemDetails&gt; &lt;Footer&gt;&lt;/Footer&gt; &lt;/Route&gt; &lt;Route path ="*"&gt; &lt;NotFound&gt;&lt;/NotFound&gt; &lt;/Route&gt; &lt;/Switch&gt; &lt;/div&gt; &lt;/Router&gt; &lt;/userContext;Provider&gt; ); } export default App;</pre><p> <strong>這是我的 FoodItemDetails.js 文件</strong></p><pre>import React, { useEffect, useState } from "react"; import { useParams } from "react-router"; // import { useParams } from "react-router"; // import PreLoader from "../PreLoader/PreLoader"; import { FontAwesomeIcon} from '@fortawesome/react-fontawesome' import { faCartArrowDown, faCheckCircle } from '@fortawesome/free-solid-svg-icons' import PreLoader from "../PreLoader/PreLoader"; const FoodItemDetails = (props) =&gt; { // const {name,shortDescription,price,images} = props.food; console.log(props, "nashir") const [currentFood, setCurrentFood] = useState({}); const {id} = useParams(); console.log(id); const [quantity, setQuantity] = useState(1); const [isSuccess, setIsSuccess] = useState(false); const [selectedBigImage, setSelectedBigImage] = useState(null); const [preloaderVisibility, setPreloaderVisibility] = useState("block"); // const [quantity, setQuantity] = useState(1); useEffect(() =&gt; { fetch("https://red-onion-backend.herokuapp.com/food/" + id).then((res) =&gt; res.json()).then((data) =&gt; { setCurrentFood(data); setPreloaderVisibility("none"); }).catch((err) =&gt; console.log(err)); if (currentFood.images) { setSelectedBigImage(currentFood.images[0]); } window.scrollTo(0, 0); }, [currentFood.name]); const finalCartHandler = (currentFood) =&gt; { currentFood.quantity = quantity; console.log(currentFood, 'food man'); props.cartHandler = currentFood; setIsSuccess(true); console.log(isSuccess, "what"); } if(isSuccess){ setTimeout(() =&gt; { setIsSuccess(false) console.log(isSuccess); }, 1500); } return ( &lt;div className="food-details container my-5"&gt; &lt;PreLoader visibility={preloaderVisibility}&gt;&lt;/PreLoader&gt; {currentFood.name &amp;&amp; ( &lt;div className="row"&gt; &lt;div className="col-md-6 my-5"&gt; &lt;h1&gt;{currentFood.name}&lt;/h1&gt; &lt;p className="my-5"&gt;{currentFood.fullDescription}&lt;/p&gt; &lt;div className="d-flex my-4"&gt; &lt;h2&gt;${currentFood.price.toFixed(2)}&lt;/h2&gt; &lt;div className="cart-controller ml-3"&gt; &lt;button className="btn" onClick={() =&gt; setQuantity(quantity &lt;= 1? 1: quantity - 1)} &gt; - &lt;/button&gt; &lt;button className="btn" onClick={() =&gt; setQuantity(quantity + 1)} &gt; + &lt;/button&gt; &lt;/div&gt; &lt;/div&gt; &lt;div className="action d-flex align-items-center"&gt; &lt;button className="btn btn-danger btn-rounded" onClick={() =&gt; finalCartHandler(currentFood)}&gt;&lt;FontAwesomeIcon icon={faCartArrowDown} /&gt; Add&lt;/button&gt; &lt;/div&gt; &lt;div className="more-images mt-5"&gt; {currentFood.images.map((img, index) =&gt; ( &lt;img onClick={() =&gt; setSelectedBigImage(currentFood.images[index])} className={ currentFood.images[index] === selectedBigImage? "mr-4 small-img active-small-img": "mr-4 small-img" } height="150px" src={img} alt="" /&gt; ))} &lt;/div&gt; &lt;/div&gt; &lt;div className="col-md-6 my-5"&gt; &lt;img src={selectedBigImage} className="img-fluid" alt="" /&gt; &lt;/div&gt; &lt;/div&gt; )} &lt;/div&gt; ); }; export default FoodItemDetails;</pre></div></object>

[英]ReactJs: TypeError: Cannot assign to read only property 'cartHandler' of object '#<Object>'?

ReactJs 類型錯誤:無法分配給 object 的只讀屬性“名稱”'#<object> '<div id="text_translate"><p> 我正在嘗試更改輸入的名稱屬性,因為我的組件的 state 發生了變化。<br> 簡而言之,這就是我想要做的。</p><pre> let displayInputElement = ( &lt;input type="text" name = {pips} placeholder="Type your answer here" className=" form-control" /&gt; ); displayInputElement.props.name = "chicken";</pre><p> 但我收到以下錯誤</p><blockquote><p>類型錯誤:無法分配給 object '#' 的只讀屬性“名稱”</p></blockquote><p> 請我如何在反應中實現以下目標</p><pre>displayInputElement.props.name = "chicken"; let pips = displayInputElement.props.name; displayInputElement = ( &lt;input type="text" name = "chicken" placeholder="Type your answer here" className=" form-control" /&gt; );</pre></div></object>

[英]ReactJs TypeError: Cannot assign to read only property 'name' of object '#<Object>'

TypeError: 無法分配給 object 的只讀屬性 'children' '#<object> '<div id="text_translate"><p> 使用 docker 構建的下一個 js 在包含 getServerSideProps package.json 的所有路由中重新加載時進入內部服務器錯誤</p><ul><li>反應:“17.0.2”</li><li> 下一個:“^11.1.2”</li></ul><p> <strong>在本地</strong>一切正常,如果我<strong>在沒有 docker 的情況下部署它</strong>。 但是在我打開網站后使用<strong>docker</strong> 。 如果我使用客戶端路由器導航,一切都很好。 但是一旦我重新加載頁面,它就會進入內部服務器錯誤並且不會重建頁面<a href="https://i.stack.imgur.com/FCgpe.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/FCgpe.png" alt="在此處輸入圖像描述"></a></p><p> 檢查 docker 日志后,我發現了這個</p><pre>TypeError: Cannot assign to read only property 'children' of object '#&lt;Object&gt;' at /app/.next/server/chunks/6859.js:792:29 at /app/node_modules/react/cjs/react.development.js:1067:17 at mapIntoArray (/app/node_modules/react/cjs/react.development.js:964:23) at mapIntoArray (/app/node_modules/react/cjs/react.development.js:1004:23) at Object.mapChildren [as map] (/app/node_modules/react/cjs/react.development.js:1066:3) at Head.makeStylesheetInert (/app/.next/server/chunks/6859.js:782:36) at Head.render (/app/.next/server/chunks/6859.js:839:23) at processChild (/app/node_modules/react-dom/cjs/react-dom-server.node.development.js:3450:18) at resolve (/app/node_modules/react-dom/cjs/react-dom-server.node.development.js:3270:5) at ReactDOMServerRenderer.render (/app/node_modules/react-dom/cjs/react-dom-server.node.development.js:3753:22) at ReactDOMServerRenderer.read (/app/node_modules/react-dom/cjs/react-dom-server.node.development.js:3690:29) at Object.renderToStaticMarkup (/app/node_modules/react-dom/cjs/react-dom-server.node.development.js:4314:27) at Object.renderToHTML (/app/node_modules/next/dist/server/render.js:711:41) at runMicrotasks (&lt;anonymous&gt;) at processTicksAndRejections (node:internal/process/task_queues:96:5) at async doRender (/app/node_modules/next/dist/server/next-server.js:1149:38)</pre></div></object>

[英]TypeError: Cannot assign to read only property 'children' of object '#<Object>'

未捕獲的類型錯誤:無法分配給 object 的只讀屬性'#<object> '<div id="text_translate"><p> 對於我目前正在處理的代碼,我有一個非常復雜的數組,里面裝滿了對象。 本頁的重點是一次更改數組中一個 object 的一個屬性,因此,需要更新整個數組以執行該更改。 該數組被初始化為...</p><pre> const [allDevices, setAllDevices] = useState(initialDeviceNames)</pre><p> 其中 intialDeviceNames 要么是從查詢中獲取的填充 object,要么是空數組(如果沒有匹配的實例)。 看起來像這樣...</p><p> <strong>請注意,空的 object(如果沒有找到具有匹配日期值的值)會將每個數字屬性設置為{id: null, name: "No Driver Selected"}</strong></p><pre> 0: 0: {id: 'dfebc7ce-ea4b-48d4-9fd9-7c2d02572e40', name: 'DANIEL STITT', type: 'Vehicle'} 1: {id: '64303dc1-0ba6-43bb-a25a-9885f9e8f2e3', name: 'KE.NETH WILLIFORD', type: 'Vehicle'} 2: {id: '1a778957-b679-401b-972d-aeb32f84e667', name: 'JASON PITSNOGLE', type: 'Vehicle'} 3: {id: '1fcc9d60-fc6f-4e34-b5ab-c64d5ea8778a', name: 'VIRGINIA SHADE', type: 'Vehicle'} 4: {id: null, name: 'No Driver Assigned'} 5: {id: null, name: 'No Driver Assigned'} 6: {id: null, name: 'No Driver Assigned'} amount: 6 name: "Vehicle" remaining_drivers: (60) [{…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}] [[Prototype]]: Object 1: 0: {id: '1a778957-b679-401b-972d-aeb32f84e667', name: 'JASON PITSNOGLE', type: 'iPad'} 1: {id: 'dfebc7ce-ea4b-48d4-9fd9-7c2d02572e40', name: 'DANIEL STITT', type: 'iPad'} 2: {id: '1fcc9d60-fc6f-4e34-b5ab-c64d5ea8778a', name: 'VIRGINIA SHADE', type: 'iPad'} 3: {id: '203726da-dba7-4f74-9d86-919d6a02a282', name: 'DONNA HAGGERTY', type: 'iPad'} 4: {id: null, name: 'No Driver Assigned'} 5: {id: null, name: 'No Driver Assigned'} 6: {id: null, name: 'No Driver Assigned'} 7: {id: null, name: 'No Driver Assigned'} 8: {id: null, name: 'No Driver Assigned'} 9: {id: null, name: 'No Driver Assigned'} 10: {id: null, name: 'No Driver Assigned'} amount: 10 name: "iPad" remaining_drivers: (60) [{…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}] [[Prototype]]: Object</pre><p> 所以一次更新這個顯然很復雜。 我必須在數組中找到正確的 object,然后在該 object 中找到正確的數字屬性,更改它,然后獲取整個 state 進行更新。 在我添加一些預填充功能之前,我為此使用的代碼一直有效。 更新這些單個屬性中的任何一個的代碼將在下面顯示...</p><pre> const handleDriverSelection = (driver, index, superIndex, deviceObj) => { // If driver is the same if (allDevices[superIndex][index].name == `${driver.firstname} ${driver.lastname}`){ } // If active driver is empty else if (allDevices[superIndex][index].name == "No Driver Assigned" || allDevices[superIndex][index] == 'undefined'){ console.log(allDevices) let newArray = [...allDevices] // The specific device drop selected will be set equal the the driver clicked newArray[superIndex][index] = { name: `${driver.firstname} ${driver.lastname}`, id: driver.id, type: deviceObj.name } // This removes the driver from the list of remaining driver newArray[superIndex].remaining_drivers = newArray[superIndex].remaining_drivers.filter( (remDriver) => { if (driver.= remDriver){ return remDriver } }) // This sets the state setAllDevices(newArray) } // if active driver exists but is NOT the one inputted else if (allDevices[superIndex][index].name.= "No Driver Assigned" ){ console.log(allDevices) let newArray = [...allDevices] // This finds the driver that was previously selected and adds him/her back to the remaining list // For each driver... user.drivers.forEach( (dspDriver) => { // if the driver iterated == the driver that was previously selected if (allDevices[superIndex][index].name == `${dspDriver.firstname} ${dspDriver.lastname}`){ // Adds the driver to remaining drivers newArray[superIndex].remaining_drivers = [..,newArray[superIndex]:remaining_drivers. dspDriver] // Sets the current drop state to the driver selected newArray[superIndex][index] = { name. `${driver,firstname} ${driver:lastname}`. id, driver:id. type. deviceObj.name } // Removes the driver selected from the list of remaining drivers newArray[superIndex].remaining_drivers = newArray[superIndex],remaining_drivers.filter( (remDriver, index) => { if (driver != remDriver){ return remDriver } }) setAllDevices(newArray) } }) } }</pre><p> 這是我感到非常困惑的地方——通過這條線</p><pre>let newArray = [...allDevices]</pre><p> 我認為 newArray 創建的數組與allDevices的內容完全相同。 allDevices ,因為它是本地 state,所以是只讀的。 我明白這一點。 我也明白,如果我寫了let newArray = allDevices ,那么newArray也將是只讀的,因為它不是一個新數組,而只是一個指向allDevices值的不同變量。 因此,我不知道為什么這個 ISNT 有效,因為newArray根本不應該是只讀的。 我添加的代碼有大量復雜的 useEffects 來處理查詢、修改和刷新,所以我看不出它會如何影響我上面顯示的代碼,特別是因為newArray除了這段代碼外不存在。</p></div></object>

[英]Uncaught TypeError: Cannot assign to read only property of object '#<Object>'

暫無
暫無

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

相關問題 未捕獲的TypeError:無法分配給對象'#<Object>'的只讀屬性'exports' 未捕獲的TypeError:無法分配為僅讀取對象&#39;的屬性&#39;exports&#39; ReactJs - TypeError:無法分配給對象“#”的只讀屬性“transform”<Object> &#39; ReactJs:類型錯誤:無法分配給 object 的只讀屬性“cartHandler”'#<object> '?<div id="text_translate"><p> 在這里,我正在嘗試建立一個餐廳網站。 我已經構建了它的一半。 但我被困在某個時刻。 我收到錯誤。 我無法在我的代碼中找出問題所在。 每當我嘗試通過單擊添加按鈕添加新食物然后我得到 TypeError: Cannot assign to read only property 'cartHandler' of object '#'?.. 我試圖在 cartHandler object 中調試,但我得到了同樣的錯誤很多次。 有人可以檢查一下嗎?</p><p> <strong>這是我的 App.js 文件</strong></p><pre>import logo from './logo.svg'; import { BrowserRouter as Router, Switch, Route, Link } from "react-router-dom"; import './App.css'; import '../node_modules/bootstrap/dist/css/bootstrap.min.css' import Banner from './components/Banner/Banner'; import Header from './components/Header/Header'; import Foods from './components/Foods/Foods'; import Features from './components/Features/Features'; import Footer from './components/Footer/Footer'; // import SIgnUp from './components/SignUp/SIgnUp'; import NotFound from './components/NotFound/NotFound'; import FoodItemDetails from './components/FoodItemDetails/FoodItemDetails' import { createContext, useContext, useState } from 'react'; import Login from './components/Login/Login'; export const userContext = createContext(); function App() { const [cart, setCart] = useState([]); const [orderId, setOrderId] = useState(null); const [deliveryDetails, setDeliveryDetails] = useState({ todoor:null,road:null, flat:null, businessname:null, address: null }); const [userEmail, setUserEmail] = useState(null); const deliveryDetailsHandler = (data) =&gt; { setDeliveryDetails(data) } const getUserEmail = (email) =&gt; { setUserEmail(email); } const clearCart = () =&gt; { const orderedItems = cart.map(cartItem =&gt; { return {food_id: cartItem.id, quantity: cartItem.quantity} }) const orderDetailsData = { userEmail, orderedItems, deliveryDetails } fetch('https://red-onion-backend.herokuapp.com/submitorder', { method: "POST", headers: { "Content-type": "application/json" }, body: JSON.stringify(orderDetailsData) }).then(res =&gt; res.json()).then(data=&gt; setOrderId(data._id)) console.log(orderId); setCart([]) } const cartHandler = (data) =&gt; { const alreadyAdded = cart.find(crt =&gt; crt.id == data.id ); const newCart = [...cart,data] setCart(newCart); if(alreadyAdded){ const reamingCarts = cart.filter(crt =&gt; cart.id;= data); setCart(reamingCarts). }else{ const newCart = [..,cart;data] setCart(newCart), } } const checkOutItemHandler = (productId. productQuantity) =&gt; { const newCart = cart.map(item =&gt; { if(item.id == productId){ item;quantity = productQuantity; } return item. }) const filteredCart = newCart.filter(item =&gt; item,quantity &gt; 0) setCart(filteredCart) } const [logggedInUser; setLoggedInUser] = useState({}), const [signOutUser; setSignOutUser] = useState({}). return ( &lt;userContext,Provider value={([logggedInUser, setLoggedInUser], [signOutUser: setSignOutUser])}&gt; &lt;Router&gt; &lt;div className="App"&gt; &lt;Switch&gt; &lt;Route exact path="/"&gt; &lt;Header&gt;&lt;/Header&gt; &lt;Banner&gt;&lt;/Banner&gt; &lt;Foods&gt;&lt;/Foods&gt; &lt;Features&gt;&lt;/Features&gt; &lt;Footer&gt;&lt;/Footer&gt; &lt;/Route&gt; &lt;Route path="/user"&gt; &lt;Login&gt;&lt;/Login&gt; &lt;/Route&gt; &lt;Route path= "/food/.id"&gt; &lt;Header cart={cart}&gt;&lt;/Header&gt; {/* &lt;FoodItemDetails cart={cart} cartHandler={cartHandler}&gt;&lt;/FoodItemDetails&gt; */} &lt;FoodItemDetails cart={cart} cartHandler={cartHandler}&gt;&lt;/FoodItemDetails&gt; &lt;Footer&gt;&lt;/Footer&gt; &lt;/Route&gt; &lt;Route path ="*"&gt; &lt;NotFound&gt;&lt;/NotFound&gt; &lt;/Route&gt; &lt;/Switch&gt; &lt;/div&gt; &lt;/Router&gt; &lt;/userContext;Provider&gt; ); } export default App;</pre><p> <strong>這是我的 FoodItemDetails.js 文件</strong></p><pre>import React, { useEffect, useState } from "react"; import { useParams } from "react-router"; // import { useParams } from "react-router"; // import PreLoader from "../PreLoader/PreLoader"; import { FontAwesomeIcon} from '@fortawesome/react-fontawesome' import { faCartArrowDown, faCheckCircle } from '@fortawesome/free-solid-svg-icons' import PreLoader from "../PreLoader/PreLoader"; const FoodItemDetails = (props) =&gt; { // const {name,shortDescription,price,images} = props.food; console.log(props, "nashir") const [currentFood, setCurrentFood] = useState({}); const {id} = useParams(); console.log(id); const [quantity, setQuantity] = useState(1); const [isSuccess, setIsSuccess] = useState(false); const [selectedBigImage, setSelectedBigImage] = useState(null); const [preloaderVisibility, setPreloaderVisibility] = useState("block"); // const [quantity, setQuantity] = useState(1); useEffect(() =&gt; { fetch("https://red-onion-backend.herokuapp.com/food/" + id).then((res) =&gt; res.json()).then((data) =&gt; { setCurrentFood(data); setPreloaderVisibility("none"); }).catch((err) =&gt; console.log(err)); if (currentFood.images) { setSelectedBigImage(currentFood.images[0]); } window.scrollTo(0, 0); }, [currentFood.name]); const finalCartHandler = (currentFood) =&gt; { currentFood.quantity = quantity; console.log(currentFood, 'food man'); props.cartHandler = currentFood; setIsSuccess(true); console.log(isSuccess, "what"); } if(isSuccess){ setTimeout(() =&gt; { setIsSuccess(false) console.log(isSuccess); }, 1500); } return ( &lt;div className="food-details container my-5"&gt; &lt;PreLoader visibility={preloaderVisibility}&gt;&lt;/PreLoader&gt; {currentFood.name &amp;&amp; ( &lt;div className="row"&gt; &lt;div className="col-md-6 my-5"&gt; &lt;h1&gt;{currentFood.name}&lt;/h1&gt; &lt;p className="my-5"&gt;{currentFood.fullDescription}&lt;/p&gt; &lt;div className="d-flex my-4"&gt; &lt;h2&gt;${currentFood.price.toFixed(2)}&lt;/h2&gt; &lt;div className="cart-controller ml-3"&gt; &lt;button className="btn" onClick={() =&gt; setQuantity(quantity &lt;= 1? 1: quantity - 1)} &gt; - &lt;/button&gt; &lt;button className="btn" onClick={() =&gt; setQuantity(quantity + 1)} &gt; + &lt;/button&gt; &lt;/div&gt; &lt;/div&gt; &lt;div className="action d-flex align-items-center"&gt; &lt;button className="btn btn-danger btn-rounded" onClick={() =&gt; finalCartHandler(currentFood)}&gt;&lt;FontAwesomeIcon icon={faCartArrowDown} /&gt; Add&lt;/button&gt; &lt;/div&gt; &lt;div className="more-images mt-5"&gt; {currentFood.images.map((img, index) =&gt; ( &lt;img onClick={() =&gt; setSelectedBigImage(currentFood.images[index])} className={ currentFood.images[index] === selectedBigImage? "mr-4 small-img active-small-img": "mr-4 small-img" } height="150px" src={img} alt="" /&gt; ))} &lt;/div&gt; &lt;/div&gt; &lt;div className="col-md-6 my-5"&gt; &lt;img src={selectedBigImage} className="img-fluid" alt="" /&gt; &lt;/div&gt; &lt;/div&gt; )} &lt;/div&gt; ); }; export default FoodItemDetails;</pre></div></object> ReactJs 類型錯誤:無法分配給 object 的只讀屬性“名稱”'#<object> '<div id="text_translate"><p> 我正在嘗試更改輸入的名稱屬性,因為我的組件的 state 發生了變化。<br> 簡而言之,這就是我想要做的。</p><pre> let displayInputElement = ( &lt;input type="text" name = {pips} placeholder="Type your answer here" className=" form-control" /&gt; ); displayInputElement.props.name = "chicken";</pre><p> 但我收到以下錯誤</p><blockquote><p>類型錯誤:無法分配給 object '#' 的只讀屬性“名稱”</p></blockquote><p> 請我如何在反應中實現以下目標</p><pre>displayInputElement.props.name = "chicken"; let pips = displayInputElement.props.name; displayInputElement = ( &lt;input type="text" name = "chicken" placeholder="Type your answer here" className=" form-control" /&gt; );</pre></div></object> 無法分配給對象“#”的只讀屬性“exports”<Object> &#39; 類型錯誤:無法分配給對象“[object Array]”的只讀屬性“0” TypeError:無法分配給 typescript 中的 object '[object Array]' 的只讀屬性 '0' TypeError: 無法分配給 object 的只讀屬性 'children' '#<object> '<div id="text_translate"><p> 使用 docker 構建的下一個 js 在包含 getServerSideProps package.json 的所有路由中重新加載時進入內部服務器錯誤</p><ul><li>反應:“17.0.2”</li><li> 下一個:“^11.1.2”</li></ul><p> <strong>在本地</strong>一切正常,如果我<strong>在沒有 docker 的情況下部署它</strong>。 但是在我打開網站后使用<strong>docker</strong> 。 如果我使用客戶端路由器導航,一切都很好。 但是一旦我重新加載頁面,它就會進入內部服務器錯誤並且不會重建頁面<a href="https://i.stack.imgur.com/FCgpe.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/FCgpe.png" alt="在此處輸入圖像描述"></a></p><p> 檢查 docker 日志后,我發現了這個</p><pre>TypeError: Cannot assign to read only property 'children' of object '#&lt;Object&gt;' at /app/.next/server/chunks/6859.js:792:29 at /app/node_modules/react/cjs/react.development.js:1067:17 at mapIntoArray (/app/node_modules/react/cjs/react.development.js:964:23) at mapIntoArray (/app/node_modules/react/cjs/react.development.js:1004:23) at Object.mapChildren [as map] (/app/node_modules/react/cjs/react.development.js:1066:3) at Head.makeStylesheetInert (/app/.next/server/chunks/6859.js:782:36) at Head.render (/app/.next/server/chunks/6859.js:839:23) at processChild (/app/node_modules/react-dom/cjs/react-dom-server.node.development.js:3450:18) at resolve (/app/node_modules/react-dom/cjs/react-dom-server.node.development.js:3270:5) at ReactDOMServerRenderer.render (/app/node_modules/react-dom/cjs/react-dom-server.node.development.js:3753:22) at ReactDOMServerRenderer.read (/app/node_modules/react-dom/cjs/react-dom-server.node.development.js:3690:29) at Object.renderToStaticMarkup (/app/node_modules/react-dom/cjs/react-dom-server.node.development.js:4314:27) at Object.renderToHTML (/app/node_modules/next/dist/server/render.js:711:41) at runMicrotasks (&lt;anonymous&gt;) at processTicksAndRejections (node:internal/process/task_queues:96:5) at async doRender (/app/node_modules/next/dist/server/next-server.js:1149:38)</pre></div></object> 未捕獲的類型錯誤:無法分配給 object 的只讀屬性'#<object> '<div id="text_translate"><p> 對於我目前正在處理的代碼,我有一個非常復雜的數組,里面裝滿了對象。 本頁的重點是一次更改數組中一個 object 的一個屬性,因此,需要更新整個數組以執行該更改。 該數組被初始化為...</p><pre> const [allDevices, setAllDevices] = useState(initialDeviceNames)</pre><p> 其中 intialDeviceNames 要么是從查詢中獲取的填充 object,要么是空數組(如果沒有匹配的實例)。 看起來像這樣...</p><p> <strong>請注意,空的 object(如果沒有找到具有匹配日期值的值)會將每個數字屬性設置為{id: null, name: "No Driver Selected"}</strong></p><pre> 0: 0: {id: 'dfebc7ce-ea4b-48d4-9fd9-7c2d02572e40', name: 'DANIEL STITT', type: 'Vehicle'} 1: {id: '64303dc1-0ba6-43bb-a25a-9885f9e8f2e3', name: 'KE.NETH WILLIFORD', type: 'Vehicle'} 2: {id: '1a778957-b679-401b-972d-aeb32f84e667', name: 'JASON PITSNOGLE', type: 'Vehicle'} 3: {id: '1fcc9d60-fc6f-4e34-b5ab-c64d5ea8778a', name: 'VIRGINIA SHADE', type: 'Vehicle'} 4: {id: null, name: 'No Driver Assigned'} 5: {id: null, name: 'No Driver Assigned'} 6: {id: null, name: 'No Driver Assigned'} amount: 6 name: "Vehicle" remaining_drivers: (60) [{…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}] [[Prototype]]: Object 1: 0: {id: '1a778957-b679-401b-972d-aeb32f84e667', name: 'JASON PITSNOGLE', type: 'iPad'} 1: {id: 'dfebc7ce-ea4b-48d4-9fd9-7c2d02572e40', name: 'DANIEL STITT', type: 'iPad'} 2: {id: '1fcc9d60-fc6f-4e34-b5ab-c64d5ea8778a', name: 'VIRGINIA SHADE', type: 'iPad'} 3: {id: '203726da-dba7-4f74-9d86-919d6a02a282', name: 'DONNA HAGGERTY', type: 'iPad'} 4: {id: null, name: 'No Driver Assigned'} 5: {id: null, name: 'No Driver Assigned'} 6: {id: null, name: 'No Driver Assigned'} 7: {id: null, name: 'No Driver Assigned'} 8: {id: null, name: 'No Driver Assigned'} 9: {id: null, name: 'No Driver Assigned'} 10: {id: null, name: 'No Driver Assigned'} amount: 10 name: "iPad" remaining_drivers: (60) [{…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}] [[Prototype]]: Object</pre><p> 所以一次更新這個顯然很復雜。 我必須在數組中找到正確的 object,然后在該 object 中找到正確的數字屬性,更改它,然后獲取整個 state 進行更新。 在我添加一些預填充功能之前,我為此使用的代碼一直有效。 更新這些單個屬性中的任何一個的代碼將在下面顯示...</p><pre> const handleDriverSelection = (driver, index, superIndex, deviceObj) => { // If driver is the same if (allDevices[superIndex][index].name == `${driver.firstname} ${driver.lastname}`){ } // If active driver is empty else if (allDevices[superIndex][index].name == "No Driver Assigned" || allDevices[superIndex][index] == 'undefined'){ console.log(allDevices) let newArray = [...allDevices] // The specific device drop selected will be set equal the the driver clicked newArray[superIndex][index] = { name: `${driver.firstname} ${driver.lastname}`, id: driver.id, type: deviceObj.name } // This removes the driver from the list of remaining driver newArray[superIndex].remaining_drivers = newArray[superIndex].remaining_drivers.filter( (remDriver) => { if (driver.= remDriver){ return remDriver } }) // This sets the state setAllDevices(newArray) } // if active driver exists but is NOT the one inputted else if (allDevices[superIndex][index].name.= "No Driver Assigned" ){ console.log(allDevices) let newArray = [...allDevices] // This finds the driver that was previously selected and adds him/her back to the remaining list // For each driver... user.drivers.forEach( (dspDriver) => { // if the driver iterated == the driver that was previously selected if (allDevices[superIndex][index].name == `${dspDriver.firstname} ${dspDriver.lastname}`){ // Adds the driver to remaining drivers newArray[superIndex].remaining_drivers = [..,newArray[superIndex]:remaining_drivers. dspDriver] // Sets the current drop state to the driver selected newArray[superIndex][index] = { name. `${driver,firstname} ${driver:lastname}`. id, driver:id. type. deviceObj.name } // Removes the driver selected from the list of remaining drivers newArray[superIndex].remaining_drivers = newArray[superIndex],remaining_drivers.filter( (remDriver, index) => { if (driver != remDriver){ return remDriver } }) setAllDevices(newArray) } }) } }</pre><p> 這是我感到非常困惑的地方——通過這條線</p><pre>let newArray = [...allDevices]</pre><p> 我認為 newArray 創建的數組與allDevices的內容完全相同。 allDevices ,因為它是本地 state,所以是只讀的。 我明白這一點。 我也明白,如果我寫了let newArray = allDevices ,那么newArray也將是只讀的,因為它不是一個新數組,而只是一個指向allDevices值的不同變量。 因此,我不知道為什么這個 ISNT 有效,因為newArray根本不應該是只讀的。 我添加的代碼有大量復雜的 useEffects 來處理查詢、修改和刷新,所以我看不出它會如何影響我上面顯示的代碼,特別是因為newArray除了這段代碼外不存在。</p></div></object>
 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM