mirror of
https://github.com/freeCodeCamp/freeCodeCamp.git
synced 2026-03-11 22:01:07 -04:00
* fix: replace sh with shell fix replace terminal with shell fix replace node with js fix replace output with shell fix replace cs with csharp fix replace c++ with cpp fix replace c# with csharp fix replace javasctipt with js fix replace syntax with js fix replace unix with shell fix replace linux with shell fix replace java 8 with java fix replace swift4 with swift fix replace react.js with jsx fix replace javascriot with js fix replace javacsript with js fix replace c++ - with cpp fix: corrected various typos fix: replace Algorithm with nothing fix: replace xaml with xml fix: replace solidity with nothing fix: replace c++ with cpp fix: replace txt with shell fix: replace code with json and css fix: replace console with shell
1.8 KiB
1.8 KiB
title, localeTitle
| title | localeTitle |
|---|---|
| Change Inline CSS Conditionally Based on Component State | Alterar Inline CSS Condicionalmente Baseado no Estado do Componente |
Alterar Inline CSS Condicionalmente Baseado no Estado do Componente
Sugestão 1:
Você estará verificando o comprimento de this.state.input então use sua propriedade .length .
this.state.input.length
Dica 2:
Você está inserindo o código antes da declaração de retorno para poder usar um JavaScript puro if/then declaração.
Solução:
Você usará uma instrução if/then para verificar o valor de this.state.input.length . Se for maior que 15, atribua '3px solid red' a inputStyle.border .
class GateKeeper extends React.Component {
constructor(props) {
super(props);
this.state = {
input: ''
};
this.handleChange = this.handleChange.bind(this);
}
handleChange(event) {
this.setState({ input: event.target.value })
}
render() {
let inputStyle = {
border: '1px solid black'
};
// change code below this line
if (this.state.input.length > 15) {
inputStyle.border = '3px solid red';
}
// change code above this line
return (
<div>
<h3>Don't Type Too Much:</h3>
<input
type="text"
style={inputStyle}
value={this.state.input}
onChange={this.handleChange} />
</div>
);
}
};
Solução
Escreva uma instrução condicional que seja avaliada de acordo com o seu estado, conforme mencionado na descrição do desafio, verifique o comprimento da entrada e atribua um novo objeto à variável inputStyle.
if (this.state.input.length > 15) {
inputStyle = {
border: '3px solid red'
}
}