【发布时间】:2023-03-11 13:40:01
【问题描述】:
这是非常基本的,但我是 JS 和 React 的新手,我需要一些关于我做错了什么的指示。我有一个想要更改的Slider 组件,并且我想在类状态中反映/存储Slider 的新值。我该怎么写?
我收到此错误:
TypeError: Cannot read properties of undefined (reading 'value')
下面的代码示例:
import React from 'react';
import { Text, View, Button } from 'react-native';
import Slider from '@react-native-community/slider';
import { TextInput } from 'react-native-paper';
class App extends React.Component {
constructor(props) {
super(props);
this.state = {
value1: 1
};
}
render() {
return(
<View>
<Text>Slider</Text>
<Slider
value={this.state.value1}
onValueChange={(e) => this.setState({value1: e.target.value})}
minimumValue={1}
maximumValue={5}
step={1}/>
</View>
);
}
}
export default App;
【问题讨论】:
-
您所在的州没有
mood属性,您刚刚在您的州设置了value1属性。 -
刚刚编辑以反映