React native React Native:如何获取设备屏幕亮度并进行渲染

React native React Native:如何获取设备屏幕亮度并进行渲染,react-native,screen-brightness,React Native,Screen Brightness,我正在创建一个React应用程序来显示设备信息。我想渲染屏幕亮度级别,而不是在控制台中。我该怎么做 DeviceBrightness.getSystemBrightnessLevel().then(function(luminous) { console.log(luminous) }) 我希望渲染屏幕亮度级别,而不是显示在控制台中从“react native device brightness”导入设备亮度; import DeviceBrightness from 'react-n

我正在创建一个React应用程序来显示设备信息。我想渲染屏幕亮度级别,而不是在控制台中。我该怎么做

DeviceBrightness.getSystemBrightnessLevel().then(function(luminous) {
    console.log(luminous)
})
我希望渲染屏幕亮度级别,而不是显示在控制台中

从“react native device brightness”导入设备亮度;
import DeviceBrightness from 'react-native-device-brightness';

export default class YourComponent extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      isLoaded: false,
      brightness: 0
    };
  }

  componentDidMount() {
    DeviceBrightness.getSystemBrightnessLevel()
      .then(luminous => {
        this.setState({
          brightness: luminous,
          isLoaded: true,
        });
      });
  }

  render() {
    const { isLoaded, brightness } = this.state;
    if (!isLoaded) {
      return {/*loading view*/}
    } else {
      return (
        <Text>{brightness}</Text>
      );
    }
  }
}
导出默认类YourComponent扩展React.Component{ 建造师(道具){ 超级(道具); 此.state={ isLoaded:false, 亮度:0 }; } componentDidMount(){ DeviceBrightness.getSystemBrightnessLevel() .然后(发光=>{ 这是我的国家({ 亮度:发光, isLoaded:是的, }); }); } render(){ const{isLoaded,brightness}=this.state; 如果(!已加载){ 返回{/*加载视图*/} }否则{ 返回( {亮度} ); } } }
}

render(){
返回(
{this.state.brightness}
);
}
}

你说的“渲染”是什么意思?你的意思是在视图中显示它?这样做的方式与在React中显示其他异步内容的方式相同。使用
setState
将值置于状态,然后在渲染方法中从状态读取值。代码不起作用。其显示亮度值为零。我在componentWillMount函数中添加了一个箭头函数,代码现在起作用。这是已更改的componentWillMount函数。谢谢你的贡献。componentWillMount(){DeviceBrightness.getSystemBrightnessLevel().then((发光)=>{this.setState({brightness:Lightning,isLoaded:true,});});
import DeviceBrightness from 'react-native-device-brightness';
export default class App extends Component{
    constructor(props){
    super(props);
    this.state = {
  isLoaded: false,
  brightness: 0,
};

}
componentWillMount() {
DeviceBrightness.getSystemBrightnessLevel()
  .then((luminous) =>{
    this.setState({
      brightness: luminous,
      isLoaded: true,
    });
  });
render() {
return (
  <View style={styles.container}>
<Text style={styles.instructions}>{this.state.brightness}</Text>
 </View>
);