我有父组件,其中也包括子组件。我将一些 Prop 从父组件传递给子组件。我的问题是,当我更改 slider 以获取父组件上的新值时,重新渲染在父组件上不起作用。这也会导致无法重新渲染子组件。
当改变 slider 时,函数被执行并且状态被改变为新值。但是这个setstate并没有触发重新渲染。我正在调试下面的代码,控制台日志是这样的(将 slider 从0更改为1);
delay -> 0
delay timetypes -> 0 (6 times executed)
delay -> 1
当我触摸可触摸的不透明度之一时,得到 this.state.delay = 0 但我通过 slider 将 0 更改为 1。
// welcome.js
export default class welcome extends Component {
constructor(props) {
super(props);
this.state = {
GridViewItems: [
{ key: "1 min game", min: 1, sec: 0 },
{ key: "3 min game", min: 3, sec: 0 },
{ key: "5 min game", min: 5, sec: 0 },
{ key: "7 min game", min: 7, sec: 0 },
{ key: "10 min game", min: 10, sec: 0 },
{ key: "15 min game", min: 15, sec: 0 }
],
value : 0
};
}
GetGridViewItem(item) {
Alert.alert(item);
}
change(value) {
console.log("change value -> " + value );
this.setState({
value: value
})}
render() {
console.log("delay -> " + this.state.value);
return (
<View style={styles.MainContainer}>
<Text style={styles.welcomeText}>Welcome ChessClock</Text>
<Text style={styles.text}>Delay Time: {this.state.value}</Text>
<Slider
style = {styles.slider}
step={1}
maximumValue={20}
value={this.state.value}
onValueChange={(val)=> this.setState({value: val})}
/>
<FlatList
data={this.state.GridViewItems}
renderItem={({ item }) => (
<TimerTypes
navigation={this.props.navigation}
BlockStyle={styles.GridViewBlockStyle}
TextStyle={styles.GridViewInsideTextItemStyle}
title={item.key}
time={item.min * 60 + item.sec}
delay = {this.state.value}
/>
)}
numColumns={2}
/>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
justifyContent: "center",
flex: 1,
margin: 10,
paddingTop: Platform.OS === "ios" ? 20 : 0
},
GridViewBlockStyle: {
justifyContent: "center",
flex: 1,
alignItems: "center",
height: 100,
margin: 5,
backgroundColor: "#05BC87"
},
GridViewInsideTextItemStyle: {
color: "#fff",
padding: 10,
fontSize: 18,
justifyContent: "center"
},
welcomeText: {
color: "#0F2284",
padding: 60,
fontSize: 30,
justifyContent: "center",
textAlign: "center"
},
text: {
fontSize: 20,
justifyContent: "center",
marginLeft:10
},
slider: {
width: '100%',
marginBottom: 25,
justifyContent: "center",
alignItems : "center"
}
});
// timeTypes
export default class timeTypes extends Component {
constructor(props) {
super(props);
this.state = {
delay : props.delay
}
}
componentWillReceiveProps(nextProps) {
this.setState({ delay: nextProps.delay });
}
render() {
console.log("delay timetypes -> " + this.props.delay);
console.log("delay timetypes -> " + this.state.delay);
return (
<TouchableOpacity onPress={() => this.props.navigation.navigate('Home', {
time: this.props.time,
delay : this.state.delay
})}
style={this.props.BlockStyle} >
<Text style={this.props.TextStyle}>
{this.props.title}
</Text>
</TouchableOpacity >
);
}
}
幸福的路径应该是当更改 slider 以获取新数字时,父级将重新渲染,子级应获取新值。
最佳答案
根据文档:
FlatList is a PureComponent which means that it will not re-render if props remain shallow-equal. Make sure that everything your renderItem function depends on is passed as a prop (e.g. extraData) that is not === after updates, otherwise your UI may not update on changes. This includes the data prop and parent component state.
因此,请尝试将 extraData={ this.state.value }
添加到您的 FlatList 组件
关于javascript - 当父状态改变时如何重新渲染子组件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55889243/