如何修复此 React 组件中的 "Cannot assign to read only property 'style' of object"?
How do I fix "Cannot assign to read only property 'style' of object" in this React component?
我正在创建自己的自定义选项卡组件。它由一个选项卡 header 组成,每个选项卡都有一个 body 部分。单击选项卡 header 时,相应 body 的样式应设置为 display:block
,所有其他样式应设置为 display:none
。
由于某些原因,我收到此错误:
Cannot assign to read only property 'style' of object
我知道我不能手动更改样式 属性,因为它似乎是 read-only 但是我该如何 fix/work 解决这个问题?
这是我的代码:
Tabs.js
import React, { Component } from 'react';
class Tabs extends Component {
constructor() {
super();
this.state = {
activeIndex: 0,
};
this.tabHeads = [];
}
// Initial composition of tab heads
componentWillMount() {
React.Children.map(this.props.children, (el, i) => {
const tab = el;
const key = `tabKey${i}`;
this.tabHeads.push((
<div role="button" onClick={e => this.onTabClick(e, i)} key={key}>{tab.props.title}</div>
));
});
}
// Called when user clicks a tab head
onTabClick(e, i) {
this.setState({ activeIndex: i });
}
render() {
// Set display none or block for each tab
React.Children.map(this.props.children, (el, i) => {
const tab = el;
let visibility = 'none';
if (i === this.state.activeIndex) visibility = 'block';
const newStyle = Object.assign({}, tab.props.style, { display: visibility });
tab.props.style = newStyle;
});
return (
<div style={this.props.style}>
{this.tabHeads}
{this.props.children}
</div>
);
}
}
export default Tabs;
用法是这样的:
render() {
const tabStyles = [
{ padding: '20px' }, // Tab 0
{ padding: '20px' }, // Tab 1
];
<Tabs>
<Tab style={tabStyles[0]} title="Tab1">
<div>Content 1</div>
</Tab>
<Tab style={tabStyles[1]} title="Tab2">
<div>Content 2</div>
</Tab>
</Tabs>
}
您尝试更改此行中的 属性 值:
tab.props.style = newStyle;
但是 React 中的 props
是只读的,你不能手动更改它们的值。请查看 React docs 了解更多详情。
要修复它,您可以使用 React.cloneElement
,它允许克隆具有将合并到现有属性中的新属性的元素:
render() {
let childrenWithNewProps = React.Children.map(this.props.children, (el, i) => {
let visibility = 'none';
if (i === this.state.activeIndex) visibility = 'block';
return React.cloneElement(el, {
style: {
display: visibility
}
}
)
});
return (
<div style={this.props.style}>
{this.tabHeads}
{childrenWithNewProps}
</div>
);
}
我正在创建自己的自定义选项卡组件。它由一个选项卡 header 组成,每个选项卡都有一个 body 部分。单击选项卡 header 时,相应 body 的样式应设置为 display:block
,所有其他样式应设置为 display:none
。
由于某些原因,我收到此错误:
Cannot assign to read only property 'style' of object
我知道我不能手动更改样式 属性,因为它似乎是 read-only 但是我该如何 fix/work 解决这个问题?
这是我的代码:
Tabs.js
import React, { Component } from 'react';
class Tabs extends Component {
constructor() {
super();
this.state = {
activeIndex: 0,
};
this.tabHeads = [];
}
// Initial composition of tab heads
componentWillMount() {
React.Children.map(this.props.children, (el, i) => {
const tab = el;
const key = `tabKey${i}`;
this.tabHeads.push((
<div role="button" onClick={e => this.onTabClick(e, i)} key={key}>{tab.props.title}</div>
));
});
}
// Called when user clicks a tab head
onTabClick(e, i) {
this.setState({ activeIndex: i });
}
render() {
// Set display none or block for each tab
React.Children.map(this.props.children, (el, i) => {
const tab = el;
let visibility = 'none';
if (i === this.state.activeIndex) visibility = 'block';
const newStyle = Object.assign({}, tab.props.style, { display: visibility });
tab.props.style = newStyle;
});
return (
<div style={this.props.style}>
{this.tabHeads}
{this.props.children}
</div>
);
}
}
export default Tabs;
用法是这样的:
render() {
const tabStyles = [
{ padding: '20px' }, // Tab 0
{ padding: '20px' }, // Tab 1
];
<Tabs>
<Tab style={tabStyles[0]} title="Tab1">
<div>Content 1</div>
</Tab>
<Tab style={tabStyles[1]} title="Tab2">
<div>Content 2</div>
</Tab>
</Tabs>
}
您尝试更改此行中的 属性 值:
tab.props.style = newStyle;
但是 React 中的 props
是只读的,你不能手动更改它们的值。请查看 React docs 了解更多详情。
要修复它,您可以使用 React.cloneElement
,它允许克隆具有将合并到现有属性中的新属性的元素:
render() {
let childrenWithNewProps = React.Children.map(this.props.children, (el, i) => {
let visibility = 'none';
if (i === this.state.activeIndex) visibility = 'block';
return React.cloneElement(el, {
style: {
display: visibility
}
}
)
});
return (
<div style={this.props.style}>
{this.tabHeads}
{childrenWithNewProps}
</div>
);
}