Skip to content

Instantly share code, notes, and snippets.

@harrisonmalone
Created August 9, 2019 06:55
Show Gist options
  • Select an option

  • Save harrisonmalone/154c8434b58b1400724a9c69f805d34f to your computer and use it in GitHub Desktop.

Select an option

Save harrisonmalone/154c8434b58b1400724a9c69f805d34f to your computer and use it in GitHub Desktop.
import React from 'react';
import './App.css';
import * as historyData from './history.json';
// import Event from './Event'
class App extends React.Component {
state = {
events: historyData.result.events
}
search = (event) => {
event.preventDefault()
const query = event.target.parentElement.children[0].value
// console.log(this.state.events)
// 1. create a filtered list (array) based on the search query, using the .filter method on this.state.events, .includes on the description string
const filtered = this.state.events.filter((event) => {
return event.description.includes(query)
})
// 2. setState to be the filtered list
this.setState({
search: filtered
})
// 3. render the filtered array in render
}
render() {
const { search } = this.state
const centerHeading = {
textAlign: 'center',
color: 'red'
}
return (
<>
<h1 style={centerHeading}>Search for 20th Century Events</h1>
<form>
<input type="text" name="search" id="search"/>
<input onClick={this.search} type="submit" value="Submit" id="submit" />
</form>
{search ? search.map((event, index) => {
return (
<>
<h3>{event.date}</h3>
<p key={index}>{event.description}</p>
</>
)
}) : null}
</>
);
}
}
// stateful component
// class, like object orientated in python
// using render(), which is a lifecycle method, kind of like an instance method in python
export default App;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment