1

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;
6
  • You can convert but before that you need to learn Reactjs then you can convert easily Commented Mar 2, 2021 at 9:18
  • Actually, I already learning React.js but i stuck here and cannt convert this code in react.js Commented Mar 2, 2021 at 9:20
  • your not mentioned where you stuck in react Commented Mar 2, 2021 at 9:21
  • And what is the issue? Is there an error message? Behavior not correct? What specifically should we be looking to help with? Is it an issue with the background color? Commented Mar 2, 2021 at 9:30
  • Yes, the issue is with the background color is not changing when I run the code. I want to do this when I select black in the dropdown menu the h1 element changes its background-color to black. and when I choose white then it changes to white background Commented Mar 2, 2021 at 9:40

1 Answer 1

1

You should fix style in h1 tag

<h1 style={{backgroundColor:`${this.state.color}`}} >{this.state.whether}</h1>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.