Azh*_*har 0 reactjs react-native react-hooks
我只是将我的类转换为函数组件。在一个函数中我想转换成状态。
constructor() {
super();
this.state = {
sections: [1, 2, 3, 4, 5, 6, 7, 8],
currentSection: 1
};
}
onItemLayout = ({ nativeEvent: { layout: { x, y, width, height } } }, section) => {
// setting each items position
this.setState({ [section]: y });
};
Run Code Online (Sandbox Code Playgroud)
我刚刚将状态转换为
const [sections] = useState([1, 2, 3, 4, 5, 6, 7, 8]);
const [currentSection, setCurrentSection] = useState(1);
Run Code Online (Sandbox Code Playgroud)
但无法理解如何转换
this.setState({ [section]: y });
Run Code Online (Sandbox Code Playgroud)
到功能组件。
const [state, setState] = useState({
sections: [1, 2, 3, 4, 5, 6, 7, 8],
currentSection: 1
});
const onItemLayout = ({ nativeEvent: { layout: { y } } }, section) => {
setState(currentState => ({
...currentState,
[section]: y
}))
};
const updateSection = (newSection) => {
setState(currentState => ({
...currentState,
currentSection: newSection
}))
};
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
583 次 |
| 最近记录: |