I've gone through the react docs and tried to implement the all of the conditional example but it doesn't seem to be working on my code. Here is my code:
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'));
I'me kinda stuck right now. I tried to use if else and ternary operator and still doesn't work. I wanted to parse the input to the library data. Here is the codepen. Any help would be appreciated