Reactjs 将本机类组件accordion反应为功能组件
下面的代码显示了一个手风琴,但我需要使用useContext,据我所知,它只适用于功能组件,我很难理解如何将其转换为功能组件Reactjs 将本机类组件accordion反应为功能组件,reactjs,react-native,Reactjs,React Native,下面的代码显示了一个手风琴,但我需要使用useContext,据我所知,它只适用于功能组件,我很难理解如何将其转换为功能组件 class AnnualFeeScreen extends Component { state = { activeSections: [], }; _renderHeader(section, index, isActive) { let meanValStatus = '#39D0B5'; if (section.meanDiff
class AnnualFeeScreen extends Component {
state = {
activeSections: [],
};
_renderHeader(section, index, isActive) {
let meanValStatus = '#39D0B5';
if (section.meanDiffVal >= 1.05) {
meanValStatus = '#FCD561';
} else if (section.meanDiffVal < 0.95) {
meanValStatus = '#bbb';
}
return (
<View style={styles.flexRow}>
<View style={{flex:2}}>
<Text style={styles.h6}>{!isActive ? '+' : '-'} {section.title}</Text>
</View>
<View style={styles.flex}>
<Text style={[styles.h6, {textAlign: 'right'}]}>{Math.round((section.titleValue) / 12, 0)} kr/mån</Text>
</View>
<View style={[styles.circleStatus, {backgroundColor: meanValStatus}]}></View>
</View>
);
};
_renderContent = section => {
return (
<View>
{section.content.map((item, key) =>
<View style={styles.accordionContent} key={key}>
<View style={styles.flexRow}>
<View style={{flex: 2}}>
<Text style={[styles.p, {fontWeight: 'normal'}]}>{item.title}</Text>
</View>
<View style={styles.flex}>
<Text style={[styles.p, {fontWeight: 'normal', textAlign: 'right'}]}>{Math.round((item.value * aptPercentage) / 12, 0)} kr/mån</Text>
</View>
</View>
</View>
)}
</View>
);
};
_updateSections = activeSections => {
this.setState({ activeSections });
};
render () {
return (
<ScrollView style={styles.backgroundPrimary} showsVerticalScrollIndicator='false'>
<View style={styles.backgroundSecondary}>
<View style={styles.container}>
<Accordion
sections={brfCostRows}
activeSections={this.state.activeSections}
renderHeader={this._renderHeader}
renderContent={this._renderContent}
onChange={this._updateSections}
sectionContainerStyle={styles.accordion}
underlayColor='transparent'
/>
</View>
</View>
</ScrollView>
);
};
};
并且认为:
<NavigationEvents onWillFocus={fetchUser} />
首先,在使用钩子将类组件转换为函数组件时,您必须注意,函数组件内部不会有
这个变量。其次,功能组件内的状态需要使用useState钩子实现
const AnnualFeeScreen()=>{
const[activeSections,setActiveSections]=useState([]);
常量渲染器(节、索引、isActive){
也就是说,Valstatus='#39D0B5';
如果(section.meanDiffVal>=1.05){
平均值='#FCD561';
}否则,如果(截面平均扩散值<0.95){
平均值='#bbb';
}
返回(
{!isActive?'+':'-'}{section.title}
{Math.round((section.titleValue)/12,0)}kr/mån
);
};
const\u renderContent=section=>{
返回(
{section.content.map((项,键)=>
{item.title}
{Math.round((item.value*aptPercentage)/12,0)}kr/mån
)}
);
};
const\u updateSections=activeSections=>{
设置活动部分(活动部分);
};
返回(
);
};
首先,在使用钩子将类组件转换为功能组件时,您必须注意,功能组件内部不会有这个变量。其次,功能组件内的状态需要使用useState钩子实现
const AnnualFeeScreen()=>{
const[activeSections,setActiveSections]=useState([]);
常量渲染器(节、索引、isActive){
也就是说,Valstatus='#39D0B5';
如果(section.meanDiffVal>=1.05){
平均值='#FCD561';
}否则,如果(截面平均扩散值<0.95){
平均值='#bbb';
}
返回(
{!isActive?'+':'-'}{section.title}
{Math.round((section.titleValue)/12,0)}kr/mån
);
};
const\u renderContent=section=>{
返回(
{section.content.map((项,键)=>
{item.title}
{Math.round((item.value*aptPercentage)/12,0)}kr/mån
)}
);
};
const\u updateSections=activeSections=>{
设置活动部分(活动部分);
};
返回(
);
};
<NavigationEvents onWillFocus={fetchUser} />
const AnnualFeeScreen = () => {