0

All:

I am pretty new to React, I am wondering if anyone could give me a comparison between createClass and extends Component

The first question is why we need React.Component since we already have createClass?

Second, something specific is:

1. What does this.state looks like in extends Component mode: I try to use this.state in constructor(), but failed as undifined, does that mean I have to build this.state myself by giving it the whole state tree?

And one question for both modes:

If I want to use setState() with a complex object(which has nested data structure) how can I only update according part, for example, say the state is like:

State = {
    title: "",
    attrs: {
        size: {
            width:"100px",
            height: "100px"
        }
    }
}

How can I only update the height using setState()? Should I use it like

setState({
    attrs: {
        size: {
            height:"200px"
        }
    }
})

Thanks

2 Answers 2

1
  1. There are plenty of answers on this topic up there: link, link

  2. The state should be initialized in constructor, before you do so - its undefined:

    public constructor(props, context)
    {
        super(props, context)
    
        this.state = {
            Clients: [],
        };
    }
    
  3. You can always update just part of the state and then force update:

    this.state.attrs.size.height = 200;
    this.forceUpdate(); 
    

    Or just update the state object and reassign it using setState again.

Sign up to request clarification or add additional context in comments.

3 Comments

thanks, another question related to this is: when I tried the tutorial in facebook site, it seems that most cases still written with createClass(), I wonder which mode is recommended: the createClass or extends Component
My personal opinion is - extends - as it is the future of javascript.
Thanks, BTW, what is that context means in constructor?
1

React.createClass was the older way to define a react component. class did not really exist within javascript at the time so this was React's implementation of 'classes';

import React, {Component} from 'react';
class ReactComponent extends Component

is the newer way to do define a react component based on es2015 classes.

State should behave the same way no matter how you define the component.

Within the constructor you should be able to define this.state.

I would look into the Flux architecture and implement either Redux or AltJS to help with the data flow architecture.

In your state if you have an object :

q = {
  title: "Test",
  body : "Test123"
}

and want to update the title ... you have to do the following :

var x = this.state.q;
x.title = "New Title";
this.setState({ q: x });

You can not modify an object attribute in state. You have to update the attribute and setState with the entire new object.

2 Comments

thanks, another question related to this is: when I tried the tutorial in facebook site, it seems that most cases still written with createClass(), I wonder which mode is recommended: the createClass or extends Component
@Kuan I would assume the facebook site still uses createClass because that doesn't rely on es2015. Not all browsers support es2015 yet so you have to use a transpiler like babel. Most "newer" examples use extends Component that I've seen. And it also depends on if you are wanting to use es2015 functionality in your JS code

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.