私は反応文書を読み、すべての条件付きサンプルを実装しようとしましたが、私のコードでは機能していないようです。ここに私のコードです:Reactでの条件
class Main extends React.Component {
constructor(props) {
super(props);
this.onChange = this.onChange.bind(this);
this.handleSubmit = this.handleSubmit.bind(this);
this.state = {items: [],text: ''};
}
onChange(e) {
this.setState({text: e.target.value});
}
handleSubmit(e) {
e.preventDefault();
let newItem = this.state.items;
let text = this.state.text;
newItem.push(text);
let newText = '';
this.setState({items: newItem, text: newText});
}
render() {
return (
<div className="container">
<div className="inside-box">
<h4>Javascript Library</h4>
</div>
<form onSubmit={this.handleSubmit}>
<input
type="text"
onChange={this.onChange}
value={this.state.text}
placeholder="Add your item..." />
</form>
{/* this will compare the input that I submitted and compare with the library. It will show result if the is a library or else it will show an error */}
{library === this.state.items && <Result /> }
</div>
)
}
}
const Result =() => {
return <h1>There is a library</h1>;
}
const Error =() => {
return <h1>There is no such library</h1>;
}
var library = ['react', 'angular', 'vue'];
ReactDOM.render(<Main />, document.getElementById('app'));
私は今ちょっと固まっています。私は他と三元演算子を使用しようとし、まだ動作しません。私は、ライブラリのデータへの入力を解析したかったのです。ここにはcodepenがあります。任意の助けが歓迎される
あなたはどこにでも誤差成分を呼び出していません。したがって、私はこのような何かにあなたをお勧めします。 'library === items'があれば' Result'を返し、そうでなければ何もしません。 –