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