Hi. So I am writing some basic React trying to make sure I “get it”.
I spent more than than I want to admit just making this:
very simple.
- App fetches a list of genres (Chuck Norris jokes) and renders Header
- Header loops over the genre array and renders GernreButtons
- GenreButtons renders a div with an onClick event
- onClick handler calls upwards with this.props.genre bound
- Header does not do anything special with the event, just pass it to App.
- event finally is handled by App handleclick and my console log cries tears of joy.
I like it but.
It feels complicated for something that simple.
Am I doing things too complicated?
I guess the middle component, Header could be a functional since it only renders and then passes the event to App.
The Button app, I am not so sure! can I make it a function? They say that calling direcly on the onClick is not that optimal.
I find React really exciting but also, it feels like it takes me ages to make anything! is that normal?
Is this the right approach in general? Sorry if I sound vague.
I really like React but it feels overcomplicated.
Im so tired now that I keep trying to use this chat in VIM mode
I paste it here in case someone just want to skim it.
class App extends Component {
constructor(props){
super(props)
this.state = {list:[]}
this.handleClick = this.handleClick.bind(this)
}
componentDidMount(){
axios.get("https://api.chucknorris.io/jokes/categories")
.then((data)=>this.setState({list: data.data}))
}
handleClick(e){
console.log(e)
}
render() {
console.log(this.state.list)
return (
<div className="App">
<Header clickytoTop={this.handleClick} data={this.state.list}/>
</div>
);
}
}
class Header extends Component {
render(){
return(
<div clasName="footer">
{this.props.data.map((genre)=>
<GenreButton clicky={this.props.clickytoTop}genre={genre}/>
)}
</div>
)
}
}
class GenreButton extends Component {
handleClick = () =>{
this.props.clicky(this.props.genre)
}
render(){
return(
<div className="genreButton" onClick={this.handleClick}>{this.props.genre}</div>
)
}
}