Exercise 3: Events & States
We're in folder 3-events-and-state/app.js.
Toggler between three "tabs" (showing three countries) on the page.
Starting file:
////////////////////////////////////////////////////////////////////////////////
// Excercise:
// - make these tabs work when you click them
////////////////////////////////////////////////////////////////////////////////
var React = require('react');
var assign = require('react/lib/Object.assign');
var DATA = [
{ name: 'USA', description: 'Land of the Free, Home of the brave' },
{ name: 'China', description: 'Lots of concrete' },
{ name: 'Russia', description: 'World Cup 2018!' },
];
var App = React.createClass({
renderTabs () {
return this.props.countries.map((country, index) => {
return (
<div style={index === 0 ? styles.activeTab : styles.tab}>
{country.name}
</div>
);
});
},
renderPanel () {
var country = this.props.countries[0];
return (
<div>
<p>{country.description}</p>
</div>
);
},
render () {
return (
<div style={styles.app}>
<div style={styles.tabs}>
{this.renderTabs()}
</div>
<div style={styles.tabPanels}>
{this.renderPanel()}
</div>
</div>
);
}
});
var styles = {};
styles.tab = {
display: 'inline-block',
padding: 10,
margin: 10,
borderBottom: '4px solid',
borderBottomColor: '#ccc',
cursor: 'pointer'
};
styles.activeTab = assign({}, styles.tab, {
borderBottomColor: '#000'
});
styles.tabPanels = {
padding: 10
};
React.render(<App countries={DATA}/>, document.body);
- What about designers, how do you integrate external HTML?- A lot of designers are actually happy to write JSX and React. Or, you can just copy-paste in the elements and do a find-replace on
class
/className
, etc. Convert the static html JSX. It is possible to pull in an external jsx file. - Browser support?
IE8+