簡體   English   中英

ReactJs - 類型錯誤:無法分配給 object 的只讀屬性“名稱”'#<object> '<div id="text_translate"><p> 我有一個對象數組:</p><pre> var subcategories = [{name:'gloves', tag:'wool'}, {name:'boots', tag: 'leather'}]</pre><p> 我只想找到 object 的索引來更改名稱或標簽。 我用這個 function:</p><pre> function setSubcat(val, index, lang){ var newArr = [] var obj = { 'name': val } subcategories.map((val, i)=>{ if(index === i){ var newObj = Object.assign(val, obj) newArr.push(newObj) } newArr.push(val) }) setSubcategories(newArr) }</pre><p> 錯誤發生在var newObj = Object.assign(val, obj)</p><p> 我認為這個錯誤意味着我不能直接改變 state,所以我必須復制一份。 我認為通過subcategories映射並將其推送到本地newArr意味着我制作了數組的副本。 但是當我想在其中更改 object 的值時它不起作用,所以我使用了 Object.assign,我認為它會從數組中深度復制特定的 object,但它也不起作用。</p><p> 我在這里錯過了什么?</p></div></object>

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

我有一個對象數組:

var subcategories = [{name:'gloves', tag:'wool'}, {name:'boots', tag: 'leather'}]

我只想找到 object 的索引來更改名稱或標簽。 我用這個 function:

function setSubcat(val, index, lang){
   var newArr = []
   var obj = {
      'name': val
   }
   subcategories.map((val, i)=>{
      if(index === i){
         var newObj = Object.assign(val, obj)
         newArr.push(newObj)
      }
      newArr.push(val)
   })
   setSubcategories(newArr)
}
           

錯誤發生在var newObj = Object.assign(val, obj)

我認為這個錯誤意味着我不能直接改變 state,所以我必須復制一份。 我認為通過subcategories映射並將其推送到本地newArr意味着我制作了數組的副本。 但是當我想在其中更改 object 的值時它不起作用,所以我使用了 Object.assign,我認為它會從數組中深度復制特定的 object,但它也不起作用。

我在這里錯過了什么?

正如評論中指出的那樣:

  • 您發布的代碼未顯示您如何創建具有不可修改屬性的 object
  • 您可以創建一個新的 object 而不是使用 Object.assign 來消除錯誤
  • 以更慣用的方式使用map function
interface TaggedItem {
    name: string,
    tag: string
}
var subcategories = [{name:'gloves', tag:'wool'}, {name:'boots', tag: 'leather'}];

function setSubcat(val: string, index: number, _lang: any){
   var obj: Partial<TaggedItem> = {
      'name': val
   }
   var newArr: TaggedItem[] = subcategories.map((val, i)=>{
      if(index === i){
        return {
            ...val,
            ...obj
        } 
      } else {
        return {...val};
        // or return val; if you don't need a full copy
      }
   })
   console.log(newArr);
}

setSubcat('newName', 0, undefined);

游樂場鏈接

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>'

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>'?

未捕獲的類型錯誤:無法分配給 object 的只讀屬性 '#<object> '<div id="text_translate"><p> 我不知道這段代碼有什么區別。 class a 是組件,示例是 example.js</p><pre> import React, {Component} from 'react'; const styles = { border: { display: 'inline-block', height: '19px', padding: '1px 8px 0', border: '2px solid', borderRadius: '12px', lineHeight: '20px', fontSize: '14px', verticalAlign: 'top', }, default: { display: 'inline-block', height: '20px', padding: '1px 10px 0', borderRadius: '10px', lineHeight: '21px', fontSize: '13px', verticalAlign: 'top', }, state: { display: 'inline-block', width: '14px', height: '13px', paddingTop: '1px', lineHeight: '14px', fontSize: '11px', color: '#fff', letterSpacing: '-0.5px', textAlign: 'center', verticalAlign: 'top', } }; class A extends Component { static defaultProps = { type: 'default', }; render() { const { label, style, type, ...other } = this.props; switch (type) { case 'border': elementStyle = styles.border; break; case 'default': elementStyle = styles.default; break; case 'state': elementStyle = styles.state; break; } return ( &lt;span style={Object.assign(elementStyle, style)} {...other}&gt;{label}&lt;/span&gt; ); } } export default A;</pre><p> 示例代碼是 example.js</p><pre> import A from './A'; export default class Example extends React.Component { render() { return ( &lt;div&gt; &lt;A style={{background: '#fe6969', color: '#fff'}} /&gt; &amp;nbsp; &lt;A style={{background: '#ff8137', color: '#fff'}} /&gt; &amp;nbsp; &lt;A style={{background: '#fcb400', color: '#fff'}} /&gt; &amp;nbsp; &lt;/div&gt; ); } }</pre><p> 此代碼錯誤是 Uncaught TypeError: Cannot assign to read only property 'background' of object '#'</p><p> 我使用 babel-loader 8、babel7、webpack4</p><p> 如果我更正 Object.assgin({}, elementStyle, style) 正在工作。 我認為重新渲染 A 組件時會發生此錯誤。 我不知道為什么這個錯誤...</p><p> 請幫我。</p></div></object>

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

類型錯誤:無法分配給 object 的只讀屬性“項目”'#<object> '<div id="text_translate"><p> 我有這個問題一天,這是我當前的 state 結構:</p><pre> const [inputFields, setInputFields] = useState([ { item: '', quantityIssued: 0, quantityRequested: '', remarks: '', unit: '', }, ])</pre><p> 當我單擊編輯按鈕時,我需要將日期填寫到我的 state 例如。</p><pre> const editHandler = (order) =&gt; { const custom = [ { item: 'test', quantityIssued: 0, quantityRequested: 7, remarks: '8', unit: '1', }, { item: 'test2', quantityIssued: 0, quantityRequested: 7, remarks: '8', unit: '1', }, ] setInputFields(custom) }</pre><p> 當我使用該自定義數據時,我可以編輯 state 的數據,但是當我嘗試從具有相同結構的服務器中獲取該數據時,我會收到錯誤消息,例如:</p><pre> const editHandler = (order) =&gt; { setInputFields(order.orderItems) }</pre><p> 雖然它們是我在上面向您展示的相同數據,但如果我編輯它,我無法編輯它說錯誤標題<strong>無法分配只讀屬性</strong></p><p>這是我的界面: <a href="https://i.stack.imgur.com/34ZlA.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/34ZlA.png" alt="在此處輸入圖像描述"></a></p></div></object>

[英]TypeError: Cannot assign to read only property 'item' 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>'

暫無
暫無

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

相關問題 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:無法分配給對象“#”的只讀屬性“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> TypeError:無法分配為只讀對象“#”的屬性“ exports” <Object> 在ReactJS中 typeError : 無法分配給對象“#”的只讀屬性“addContactHandler”<Object> &#39; 未捕獲的類型錯誤:無法分配給 object 的只讀屬性 '#<object> '<div id="text_translate"><p> 我不知道這段代碼有什么區別。 class a 是組件,示例是 example.js</p><pre> import React, {Component} from 'react'; const styles = { border: { display: 'inline-block', height: '19px', padding: '1px 8px 0', border: '2px solid', borderRadius: '12px', lineHeight: '20px', fontSize: '14px', verticalAlign: 'top', }, default: { display: 'inline-block', height: '20px', padding: '1px 10px 0', borderRadius: '10px', lineHeight: '21px', fontSize: '13px', verticalAlign: 'top', }, state: { display: 'inline-block', width: '14px', height: '13px', paddingTop: '1px', lineHeight: '14px', fontSize: '11px', color: '#fff', letterSpacing: '-0.5px', textAlign: 'center', verticalAlign: 'top', } }; class A extends Component { static defaultProps = { type: 'default', }; render() { const { label, style, type, ...other } = this.props; switch (type) { case 'border': elementStyle = styles.border; break; case 'default': elementStyle = styles.default; break; case 'state': elementStyle = styles.state; break; } return ( &lt;span style={Object.assign(elementStyle, style)} {...other}&gt;{label}&lt;/span&gt; ); } } export default A;</pre><p> 示例代碼是 example.js</p><pre> import A from './A'; export default class Example extends React.Component { render() { return ( &lt;div&gt; &lt;A style={{background: '#fe6969', color: '#fff'}} /&gt; &amp;nbsp; &lt;A style={{background: '#ff8137', color: '#fff'}} /&gt; &amp;nbsp; &lt;A style={{background: '#fcb400', color: '#fff'}} /&gt; &amp;nbsp; &lt;/div&gt; ); } }</pre><p> 此代碼錯誤是 Uncaught TypeError: Cannot assign to read only property 'background' of object '#'</p><p> 我使用 babel-loader 8、babel7、webpack4</p><p> 如果我更正 Object.assgin({}, elementStyle, style) 正在工作。 我認為重新渲染 A 組件時會發生此錯誤。 我不知道為什么這個錯誤...</p><p> 請幫我。</p></div></object> TypeError:無法分配給 object '[object Array]' 的只讀屬性 'x' 類型錯誤:無法分配給 object 的只讀屬性“項目”'#<object> '<div id="text_translate"><p> 我有這個問題一天,這是我當前的 state 結構:</p><pre> const [inputFields, setInputFields] = useState([ { item: '', quantityIssued: 0, quantityRequested: '', remarks: '', unit: '', }, ])</pre><p> 當我單擊編輯按鈕時,我需要將日期填寫到我的 state 例如。</p><pre> const editHandler = (order) =&gt; { const custom = [ { item: 'test', quantityIssued: 0, quantityRequested: 7, remarks: '8', unit: '1', }, { item: 'test2', quantityIssued: 0, quantityRequested: 7, remarks: '8', unit: '1', }, ] setInputFields(custom) }</pre><p> 當我使用該自定義數據時,我可以編輯 state 的數據,但是當我嘗試從具有相同結構的服務器中獲取該數據時,我會收到錯誤消息,例如:</p><pre> const editHandler = (order) =&gt; { setInputFields(order.orderItems) }</pre><p> 雖然它們是我在上面向您展示的相同數據,但如果我編輯它,我無法編輯它說錯誤標題<strong>無法分配只讀屬性</strong></p><p>這是我的界面: <a href="https://i.stack.imgur.com/34ZlA.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/34ZlA.png" alt="在此處輸入圖像描述"></a></p></div></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:無法分配為只讀對象&#39;#的屬性&#39;background&#39; <Object> “
 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM