Reputation: 15
I want to convert javascript code to Reactjs. How i write this kind of code in React.js. Help me to explain how i do this in react.js. I also mention my react code, where I did wrong in the code and what can in do to run my code properly.
const select = document.querySelector('select');
const html = document.querySelector('html');
document.body.style.padding = '10px';
function update(bgColor, textColor) {
html.style.backgroundColor = bgColor;
html.style.color = textColor;
}
select.onchange = function() {
( select.value === 'black' ) ? update('black','white') : update('white','black');
}
<--Html--> This is Html Code which i want to convert it all in jsx file i React.js
<html>
<body>
<label for="theme">Select theme: </label>
<select id="theme">
<option value="white">White</option>
<option value="black">Black</option>
</select>
<h1>This is my website</h1>
</body>
</html>
<-- React.js--> where I did wrong in the code and what can in do to run my code properly.
import React from 'react';
import './App.css';
class App extends React.Component{
constructor(){
super();
this.state={
whether:'',
color:''
}
}
change=(event)=>{
this.setState({whether: event.target.value});
}
handleChange=(event)=>{
this.setState({color: event.target.value});
}
render(){
let pop=this.state.color;
return(
<div>
<label for="weather">Select the weather type today: </label>
<select id="weather" onChange={this.change} >
<option value="">--Make a choice--</option>
<option value="Its sunny Today">Sunny</option>
<option value="ITS Rainy Today">Rainy</option>
<option value="snowing">Snowing</option>
<option value="overcast">Overcast</option>
</select>
<label for="theme">Select theme: </label>
<select id="theme" onChange={this.handleChange} >
<option value="white">White</option>
<option value="black">Black</option>
</select>
<h1 style={{backgroundColor:{this.state.color}}} >{this.state.whether}</h1>
</div>
)
}
}
export default App;
Upvotes: 1
Views: 3018
Reputation: 36
You should fix style in h1 tag
<h1 style={{backgroundColor:`${this.state.color}`}} >{this.state.whether}</h1>
Upvotes: 1