React Native: как получить яркость экрана устройства и визуализировать ее - PullRequest
0 голосов
/ 27 марта 2019

Я создаю приложение React для отображения информации об устройстве.Я хочу сделать уровень яркости экрана, а не в консоли.Как мне это сделать?

DeviceBrightness.getSystemBrightnessLevel().then(function(luminous) {
    console.log(luminous)
})

Я ожидал отрендерить уровень яркости экрана, а не отобразить в консоли

Ответы [ 2 ]

0 голосов
/ 28 марта 2019
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>
);

} }

0 голосов
/ 27 марта 2019
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>
      );
    }
  }
}
...