Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

133
Visualizações
How user string input will show as background color in a div in react JS functional component

I am trying to make a color box where users can input color as a string but will display output as a background color in react I tried this way but it did not work as expected I am just learning to react. thanks in advance Here is my code.

import "./styles.css";
import randomColor from "randomcolor";
import { useState } from "react";
let color = randomColor();
// const name = "rocky";
export default function App() {
  const [statecolor, setcolor] = useState({
    bg:"",
    dis:false
  });

  function displayColor(e) {
    const inputcolor = e.target.value;
    if (inputcolor === color) {
      setcolor( prevValues => {
        return { ...prevValues,bg:color,dis:true}
      
     } )
    }else{
      console.log("not found")
    }
  }
  return (
    <div className="App">
      <h1>show color with input matching</h1>
      {statecolor.map((statecolors)=>{
        return (
          <h2 style={{backgroundColor:statecolors}}>{statecolors}</h2>
        )
      })}
      
     
      <input type="text" value={statecolor} onChange={displayColor} />
    </div>
  

); }

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

You're not seeing the background color because you're not setting it to a proper value. statecolors is an object, but you're setting the entire object as the value in your code. You need to access statecolors.bg and set that. Assuming the user enters it as a proper value, hex or color, then the below would work:

      {statecolor.map((colorObject)=>{
        return (
          <h2 style={{backgroundColor:colorObject.bg}}>{statecolors}</h2>
        )
      })}
about 4 years ago · Juan Pablo Isaza Relatório

0

I'm not sure this is what you want to achieve, but here's what I wrote https://codesandbox.io/s/laughing-thunder-jlc28?file=/src/App.js

  • randomColor is unused actually because you doesn't want to display initially right?
  • I'm not sure why checking this inputcolor === color, it will always go to else (unless you know what the randomColor will be) so I skipped that part
about 4 years ago · Juan Pablo Isaza Relatório

0

function displayColor(e) {
    const inputcolor = e.target.value;
    if (inputcolor) {
      setcolor( prevValues => {
        return { ...prevValues,bg:inputcolor,dis:true}
      
     } )
    }else{
      console.log("not found")
    }
  }

======================

{statecolor.map((colorObject)=>{
        return (
          <h2 style={{backgroundColor:colorObject.bg}}>{statecolors}</h2>
        )
      })}
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda