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

178
Visualizações
How can I set 2 values for one variable, and the component will render with one value, depending of some prop input

I'm new here, So if my question is not good, Please let me know so that I can edit.

I'm using ReactJS + Material UI. I have a component, but I want this component to be rendered with different properties depending on the props, like this:

In the page where I want render the component:

<AdBanner vertical={true} />

Inside my AdBanner component I have:

export default function AdBanner(props) {
  const [adWidth, setAdWidth] = useState("100%");
  const [adHeight, setAdHeight] = useState("90px");
  const [adSpacing, setAdSpacing] = useState(2);
  const [adDirection, setAdDirection] = useState("row");

  useEffect(() => {
    if (props.vertical) {
      console.log("ola");
      setAdWidth("320px");
      setAdHeight("480px");
      setAdSpacing(5);
      setAdDirection("column");
    }
  }, [props.vertical]);

  return (
    <>
      <Paper
        variant="outlined"
        sx={{ width: { xs: "100%", md: adWidth }, overflow: "hidden" }}
      >
      ...

My goal is, when I don't specify a value for the "vertical" property my component has certain characteristics (like height, width, ... ). But in some parts of my application I want a set of others values ​​for the same property.

I was getting some errors, with the help of #guilfer I was able to eliminate these errors.

Can anyone tell me if what I did is correct? Thanks.

Here the full code: https://github.com/brunovjk/saude-vapor

Thank you.

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

0

There are some things on your code that you can't actualy do in javascript. When a variable is defined using the "var", it has global scope. Here is an explanation about the differences: https://www.freecodecamp.org/news/var-let-and-const-whats-the-difference/

After that, there are some other things that we don't actualy do in React, usualy in your case, there would be two different classes with the style you want, then, for aplying the style you could do something similar to this:

import style from "style.module.css"

export default function AdBanner(props) {

  return <div className={props.vertical? style.firstClass : style.secondClass}>banner content<div>
}
.firstClass {
 ...one css styles
 }
 
 .secondClass {
 ...another css style
 }

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