简体   繁体   English

React js无法分配给只读属性

[英]React js Cannot assign to read only property

I have two components as category and category content.Category is the parent component.我有两个组件作为类别和类别内容。类别是父组件。

function handlePageSize(pageValue) {
    props.pageSize = pageValue;
    // React.cloneElement(element, pageSize = pageValue)
}
    return (
        <Fragment>
            <Meta name="description" content={metaDescription} />
            <CategoryContent
                categoryId={id}
                classes={classes}
                data={loading ? null : data}
                pageControl={pageControl}
                sortProps={sortProps}
                onSelectSize = {handlePageSize}
            />
        </Fragment>
    );
};

Category.propTypes = {
    classes: shape({
        gallery: string,
        root: string,
        title: string
    }),
    id: number,
    pageSize: number
};

Category.defaultProps = {
    id: 3,
    // TODO: This can be replaced by the value from `storeConfig when the PR,
    // https://github.com/magento/graphql-ce/pull/650, is released.
    pageSize: 8
};

In category component there exists the pageSize prop.I need to change that value when I select a dropdown value in my child component categorycompontents.Following is child component在类别组件中存在 pageSize 属性。当我在我的子组件类别组件中选择一个下拉值时,我需要更改该值。以下是子组件

    let selectedValue = null;
    function onChangePageSize(event) {
        selectedValue = event.target.value
        onSelectSize(selectedValue);
    }
   <select id="lang" onChange={onChangePageSize} defaultValue={8} value={selectedValue}>
                            <option value="8">8</option>
                            <option value="16">16</option>
                            <option value="24">24</option>
                        </select>

I get the error 'Cannot assign to read only property' when I select the value.Is there any other way to do this.I need to change number of items displayed in the page instantly when I select my dropdown value.当我选择值时,我收到错误“无法分配给只读属性”。有没有其他方法可以做到这一点。当我选择下拉值时,我需要立即更改页面中显示的项目数。

You can not update the prop's properties value (React props are immutable).您不能更新道具的属性值(React 道具是不可变的)。

What you can do is: put pageSize in a state and update it.您可以做的是:将pageSize置于状态并更新它。

Here is an example:下面是一个例子:

// import useState hook
import { useState } from 'react';

// create a state
const [pageSize, setPageSize] = useState(8); // default value

// to update the value use `setPageSize(val)`
function handlePageSize(pageValue) {
   setPageSize(pageValue)
}

// to access the value, just use `pageSize`
console.log(pageSize)

无法分配给 # 的只读属性<object>使用带有 Recoil 的 React<div id="text_translate"><p> I'm trying to update my state (an array of objects), but get the above error whenever I try to use.map or edit a clone of the state object.</p><pre> React.useEffect(() =&gt; { setUserMeasurements((oldUserMeasurements) =&gt; { return oldUserMeasurements.map(nameAndMeasure =&gt; { if (nameAndMeasure.name === name) { nameAndMeasure.measure = 60 } return nameAndMeasure; }) })</pre><p> })</p><p> 当我尝试代码的“nameAndMeasure.measure = 60”部分时,它似乎不喜欢它,但我不明白为什么。 谁能解释一下?</p></div></object> - Cannot assign to read only property of #<Object> using React with Recoil

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法分配给 React.js 中的只读属性 - Cannot assign to read only property in React.js TypeError:无法分配给 React JS 中的只读属性 - TypeError: Cannot assign to read only property in react JS 无法分配给 # 的只读属性“.js”<Object> - Cannot assign to read only property '.js' of #<Object> FCM React.js/Node npm:TypeError:无法分配给对象“Error”的只读属性“toJSON” - FCM React.js/Node npm: TypeError: Cannot assign to read only property 'toJSON' of object 'Error' React JS V17.0.0:无法分配给对象“#”的只读属性“颜色”<Object> &#39; - React JS V17.0.0: Cannot assign to read only property 'color' of object '#<Object>' 无法分配给只读属性 - Cannot assign to read only property 无法分配给 # 的只读属性“道具”<Object> 在反应原生 - Cannot assign to read only property 'props' of #<Object> in react native 无法分配给 React useRef 中的只读属性“当前” - Cannot assign to read only property 'current' in React useRef 无法分配给 # 的只读属性<object>使用带有 Recoil 的 React<div id="text_translate"><p> I'm trying to update my state (an array of objects), but get the above error whenever I try to use.map or edit a clone of the state object.</p><pre> React.useEffect(() =&gt; { setUserMeasurements((oldUserMeasurements) =&gt; { return oldUserMeasurements.map(nameAndMeasure =&gt; { if (nameAndMeasure.name === name) { nameAndMeasure.measure = 60 } return nameAndMeasure; }) })</pre><p> })</p><p> 当我尝试代码的“nameAndMeasure.measure = 60”部分时,它似乎不喜欢它,但我不明白为什么。 谁能解释一下?</p></div></object> - Cannot assign to read only property of #<Object> using React with Recoil Three.js 四元数不能分配给只读属性 - Three.js Quaternion cannot assign to read only property
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM