【发布时间】:2021-05-20 06:16:26
【问题描述】:
我知道 this.refs 已被弃用,可以使用 React.createRef 更改它,但在我的情况下是不同的
这是我们的代码
export default class ScrollableTabString extends Component {
static ITEM_PADDING = 15;
static defaultProps = {
tabs: [],
themeColor: '#ff8800',
};
static propTypes = {
tabs: PropTypes.any,
themeColor: PropTypes.string,
};
shouldComponentUpdate(nextProps, nextState) {
if (nextProps.tabs !== this.props.tabs || this.state !== nextState) {
return true;
}
return false;
}
constructor(props) {
super(props);
this.views = [];
this.state = {};
this.scrollableTabRef = React.createRef();
}
componentDidMount() {
setTimeout(() => {
this.select(this.props.defaultSelectTab ? this.props.defaultSelectTab : 0);
}, 300);
}
select(i, code) {
let key = 'tab ' + i;
for (let view of this.views) {
if (view) {
let isSelected = false;
if (view.key === key) {
isSelected = true;
}
// This refs is Object with many ref views
if (this.refs[view.key]) {
this.refs[view.key].select(isSelected);
}
this.scrollableTabRef.current.goToIndex(i);
}
}
if (this.props.onSelected) {
this.props.onSelected(code);
}
}
render() {
this.views = [];
if (this.props.tabs) {
let tabs = this.props.tabs;
for (let i = 0; i < tabs.length; i++) {
if (tabs[i]) {
let key = 'tab ' + i;
let view = (
<TabItem
column={tabs.length}
themeColor={this.props.themeColor}
useTabVersion2={this.props.useTabVersion2}
isFirstItem={i === 0}
isLastItem={i === tabs.length - 1}
ref={key}
key={key}
tabName={tabs[i].name}
onPress={() => {
this.select(i, tabs[i].code);
}}
/>
);
this.views.push(view);
}
}
}
let stypeTab = this.props.useTabVersion2
? null
: { borderBottomWidth: 0.5, borderColor: '#8F8E94' };
return (
<ScrollableTab
ref={this.scrollableTabRef}
style={Platform.OS === 'ios' ? stypeTab : null}
contentContainerStyle={Platform.OS === 'android' ? stypeTab : null}
>
{this.views}
</ScrollableTab>
);
}
}
我想修复来自 eslint 的警告,但在我们的例子中,我不能使用 React.createRef
【问题讨论】:
-
嗨@Anhdevit,您介意详细说明您的问题吗?我无法理解您想要实现什么或遇到了什么问题。谢谢。
-
对不起@WingChoy 我更新了我的问题希望它更清楚
-
1. createRef() 是否正常工作? 2. 你的应用中显示的 eslint 错误是什么? 3. 你能告诉我你正在归档的布局吗?
-
这可能会对您有所帮助。 stackoverflow.com/a/54633947/9868549
-
我可以知道您是否正在尝试制作具有不同内容的可滚动标签栏吗?
标签: reactjs react-native