Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletter Hub
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
React.js Essentials

You're reading from   React.js Essentials A fast-paced guide to designing and building scalable and maintainable web apps with React.js

Arrow left icon
Product type Paperback
Published in Aug 2015
Publisher Packt
ISBN-13 9781783551620
Length 208 pages
Edition 1st Edition
Languages
Tools
Arrow right icon
Author (1):
Arrow left icon
Artemij Fedosejev Artemij Fedosejev
Author Profile Icon Artemij Fedosejev
Artemij Fedosejev
Arrow right icon
View More author details
Toc

Table of Contents (12) Chapters Close

Preface 1. Installing Powerful Tools for Your Project 2. Create Your First React Element FREE CHAPTER 3. Create Your First React Component 4. Make Your React Components Reactive 5. Use Your React Components with Another Library 6. Update Your React Components 7. Build Complex React Components 8. Test Your React Application with Jest 9. Supercharge Your React Architecture with Flux 10. Prepare Your React Application for Painless Maintenance with Flux Index

Creating your first stateful React component

Stateful components are the most appropriate place for your application to handle the interaction logic and manage the state. They make it easier for you to reason out how your application works. This reasoning plays a key role in building maintainable web applications.

React stores the component's state in this.state, and it sets the initial value of this.state to the value returned by the getInitialState() function. However, it's up to us to tell React what the getInitialState() function will return. Let's add this function to our React component:

{
  getInitialState: function () {
    return {
      isHidden: false
    };
  },

  render: function () {
    if (this.state.isHidden) {
      return null;
    }

    return React.createElement('h1', { className: 'header' }, 'React Component');
  }
}

In this example, our getInitialState() function returns an object with a single isHidden property that is...

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image