- The
rendermethod is the only required method
- Place everything you need to display within
render
class Counter extends React.Component {
render() {
return (<div>
<h1>Your count</h1>
</div>);
}
}- Components get rendered by importing them and using their JSX syntax
- This can occur by calling
ReactDOM.renderat the top-level, or by any child components of the root in theirrenderfunctions, i.e.:
import Counter from './Counter';
class App extends React.Component {
render() {
return <Counter />;
}
}https://tinyurl.com/web-team-inline
Write a component that has some information about yourself inside of src/AboutMe.js!
Include your name in a heading, a picture of yourself (upload to the assets folder), and a little introduction blurb.
- Props are used to communicate values between parents and children.
- Props can allow your component to hold different values, depending on what your parent says
import Counter from './Counter';
class App extends React.Component {
render() {
// startCount available to an instance of the
// Counter component as this.props.startCount
return <Counter startCount={3} />;
}
}String(no{}to go into JS context needed)
<Counter message="Start count!" />Number(requires{})
<Counter startCount={3} />Function(requires{})
<Counter onNextCount={(count) => alert(count)} />Object(2 sets of{}- why?)
<Counter info={{ startCount: 3 }} />Copy your AboutMe component render function into src/AboutPerson.js. Modify this code to display prop values instead of fixed values!
-
Reminder: props can be accessed within a component instance through the
this.propsobject. -
Hint: You'll need to edit
src/PeopleContainer.jsto pass down your props correctly!
- State is used to store a component instance's current internal data
class Counter extends React.Component {
constructor(props) {
super(props);
this.state = { count: props.initialCount || 0 };
}
onClick() {
this.setState({ count: this.state.count + 1 });
}
render() {
return (<div>
<button onClick=(() => this.onClick())>Click!</button>
<p>{this.state.count}</p>
</div>);
}
}- Never touch
this.stateto set it=to something! It won't work.
- Use
this.setStateto change what state is
setStatetakes a single object as an argument and applies the changes only.
Open up src/PeopleEditable.js and implement deletion of friends from side buttons next to <AboutPerson> components!
Bonus: Add a text box next to <AboutPerson> components and allow them to modify the names of people.
- Static site generator for React
- Takes a React codebase and strips down features to make it efficient (it's a static site, after all)
- Adds built-in tools to make routing, paging, and pulling from external content sources much, much easier.
- All components exported from JS files inside of the
src/pagesdirectory become routes- i.e.,
src/pages/about.jsis served at[your url]/about
- i.e.,
- Use the
<Link>component exported bygatsby-linkfor internal links (not<a>)- i.e.,
<Link to="/about">About</Link>
- i.e.,
- Place components used by all pages (or a set of pages) in
src/layouts/
- Export GraphQL queries from components to pull in data from external sources
- External data will be available to us as if passed in via
this.props(!!)
- Integrating with different content management systems to fetch and display this external data
-
Node.js (>= 6.x) - https://nodejs.org
-
Gatsby CLI -
npm i -g gatsby-cli- If you don't like global dependencies, you can use
npm i -D gatsby-cliandnpx gatsbyinside of a project folder to generate your starter code.
- If you don't like global dependencies, you can use
- GitHub - sign up for an account and join the InnoD-WebTier organization
Create a repository on GitHub and push a new Gatsby starter project to it!
Using this new Gatsby project,