反应原生动态样式,不重新渲染整个组件
问题描述:
下面的这种条件样式仍然适用于反应原生?反应原生动态样式,不重新渲染整个组件
style={[styles.base, this.state.active && styles.background]}
安装组件时,活动状态设置为false。
屏幕上有一个按钮可将活动状态更改为true。然后,我期望显示背景风格。但事实并非如此,除非页面重新加载。
有没有想法?
感谢
答
这听起来像你们会有兴趣从反应母语使用动画模块。通过使用动画模块,您可以创建更改或动画的样式。你也可以使用LayoutAnimation。您应该阅读Animation Documentation和LayoutAnimation documentation。
你可以通过使用LayoutAnimation开始,看看你是否需要。设置起来很快!
下面是一个例子:
import React, { Component} from 'react';
import { View, LayoutAnimation, UIManager, Platform } from 'react-native';
class MyComponent extends Component {
constructor(props) {
super(props);
if (Platform.OS === 'android') {
UIManager.setLayoutAnimationEnabledExperimental(true);
}
componentWillUpdate() {
LayoutAnimation.spring() // automatimagically animates style changes
}
render() {
<View style={[styles.base, this.state.active && styles.background]}>
</View>
}
}
感谢。看起来像我需要的东西。我会尝试一下,让你知道。 – sbkl
完美地工作。谢谢!通过将平台添加到来自react-native的导入来编辑您的答案。 – sbkl
哦!我不敢相信我错过了! :) –