in Education by
I'm trying to use the sensor's API with react and I can't seen to be able to make it work. It gives me an error saying that AmbientLightSensor (in my case this sensor) is undefined If I run the script outside react ( more exactly with an extension from VSCode "live server" ) it works fine ( just a html with some JS code in it ). That's cool and all but in this case at least I want to run this script inside react and it just doesn't let me. So far I've tried: running this code in react as a class method called by componentDidMount and that (Simply i've put my JS code in there ^^ ) running this code with the tag hoping that maybe react isn't really using the JS that I know and that maybe running it inside html will change this ... No it didn't do the trick So at this point I'm unsure what to even check to make this work Here is my code, the js code I'm trying to run is inside the Did mount component class App extends Component { constructor(props) { super(props); } componentDidMount() { const details = document.getElementById("details"); // Feature detection if (window.AmbientLightSensor) { try { const sensor = new AmbientLightSensor(); // Detect changes in the light sensor.onreading = () => { details.innerHTML = sensor.illuminance; // Read the light levels in lux // < 50 is dark room if (sensor.illuminance < 50) { document.body.className = "darkLight"; } else { document.body.className = "brightLight"; } }; // Has an error occured? sensor.onerror = event => (document.getElementById("details").innerHTML = event.error.message); sensor.start(); } catch (err) { details.innerHTML = err.message; } } else { details.innerHTML = "It looks like your browser doesnt support this feature"; } } render() { return (

Ambient Light Sensor

Current Light Levels

); } } And also here is the working html Ambient Light Sensor

Ambient Light Sensor

Current Light Levels

const details = document.getElementById("details"); // Feature detection if (window.AmbientLightSensor) { try { const sensor = new AmbientLightSensor(); // Detect changes in the light sensor.onreading = () => { details.innerHTML = sensor.illuminance; // Read the light levels in lux // < 50 is dark room if (sensor.illuminance < 50) { document.body.className = "darkLight"; } else { document.body.className = "brightLight"; } }; // Has an error occured? sensor.onerror = event => (document.getElementById("details").innerHTML = event.error.message); sensor.start(); } catch (err) { details.innerHTML = err.message; } } else { details.innerHTML = "It looks like your browser doesnt support this feature"; } ``` PS* for this to work you need to run this on a https server JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
That's not how React works... I'd suggest looking into dangerouslySetInnerHTML link here: https://reactjs.org/docs/dom-elements.html#dangerouslysetinnerhtml And createRef link here: https://reactjs.org/docs/refs-and-the-dom.html#creating-refs Here is a simple example utilizing both to give you a better idea: import React from 'react'; class App extends React.Component { constructor(props) { super(props); this.detailsRef = React.createRef(); } createMarkup() { return { __html: 'whatever you want...' }; } componentDidMount() { console.log(this.detailsRef.current.innerHTML); } render() { return (

Ambient Light Sensor

Current Light Levels

); } } export default App; Play around with it and read the links from the official docs to adapt to your specific use case...

Related questions

0 votes
    I'm trying to use the sensor's API with react and I can't seen to be able to make it work ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 12, 2022 in Education by JackTerrance
0 votes
    This question already has answers here: How to split a string with angularJS (4 answers) Closed 5 years ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 14, 2022 in Education by JackTerrance
0 votes
    This question already has answers here: How to split a string with angularJS (4 answers) Closed 5 years ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 3, 2022 in Education by JackTerrance
0 votes
    This question already has answers here: How to split a string with angularJS (4 answers) Closed 5 years ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 19, 2022 in Education by JackTerrance
0 votes
    This question already has answers here: When to use ":"(colon) operator in javascript vs "=" operator ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 3, 2022 in Education by JackTerrance
0 votes
    This question already has answers here: When to use ":"(colon) operator in javascript vs "=" operator ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 2, 2022 in Education by JackTerrance
0 votes
    I created a raycaster that allows me to pick an object from the scene with my mouse like such: ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 9, 2022 in Education by JackTerrance
0 votes
    I have a function that has more than 1400+ crypto pairs and I have to send an API against each ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 5, 2022 in Education by JackTerrance
0 votes
    This is part of the component : import MyComp from '../../lib/MyComp' const Data = ( { data } ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 26, 2022 in Education by JackTerrance
0 votes
    This is part of the component : import MyComp from '../../lib/MyComp' const Data = ( { data } ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 20, 2022 in Education by JackTerrance
0 votes
    I'm importing Popup from react-leaflet import { Marker, Map, Popup, TileLayer, ZoomControl, GeoJSON, ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 14, 2022 in Education by JackTerrance
0 votes
    I have a REACTSjs application and am trying to solve a problem with localization. I receive some text from ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 7, 2022 in Education by JackTerrance
0 votes
    Different JavaScript Questions and Answers...
asked Sep 17, 2022 in Technology by Editorial Staff
0 votes
    I've created a getSpectrum method using the getByteFrequencyData method on the Web Audio API's Analyser Node ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 27, 2022 in Education by JackTerrance
0 votes
    Have jquery dialog as closeOnescape as false. want to trigger an event based on esc key press how do ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 20, 2022 in Education by JackTerrance
...