Coder Social home page Coder Social logo

Comments (4)

arunoda avatar arunoda commented on June 26, 2024
  • Use stateless components as possible as you can
  • Use React.Component if you can't (specially when we've event handlers)
  • Avoid React.createClass()

from mantra.

arunoda avatar arunoda commented on June 26, 2024

I hope we can close this.

from mantra.

nadeemja avatar nadeemja commented on June 26, 2024

Apologies for re-opening this old thread.
However, I've got some troubles related to the topic.

In short, I'm trying to implement a simple dialog from the Material-UI library.
Link: http://www.material-ui.com/#/components/dialog

  1. If I use stateless components, then I have to store state in LocalState. Which is OK, but whenever I change anything in LocalState, everything re-renders. Even with only a few elements - 4 to be exact, this is noticeably slower. Also, using this approach becomes a nightmare when trying to use react-storybook.
  2. If I use React.Component, then either there are parse errors from official examples, or this.setState() is undefined if I use constructor(). I've googled all the relevant errors, and applied all the suggested fixes (using .bind() et. al), to no avail.
  3. If I use React.createClass, then everything works! There are no multiple re-renders nor laggy-interface. Using it with react-storybook is a dream. It just works.

So, I have to ask, @arunoda:

  1. Why should I avoid React.createClass?
  2. How would you implement the state management needed for the element from Material-UI?

I'm a beginner and I'm sincerely eager to learn.
Thanks for all your hard work.
Really looking forward to your answer.

Regards,

Nadeeem

from mantra.

achtan avatar achtan commented on June 26, 2024

here is a simple example of React.Component :

class Checkbox extends React.Component {
  constructor(props) {
    super(props);

    this.state = {
      checked: props.defaultValue,
    };

    this.handleValuesChange = this.handleValuesChange.bind(this);
  }

  handleValuesChange() {
    const state = {
      checked: !this.state.checked,
    };
    this.setState(state);
    this.props.onChange(state);
  }

  render() {
    return <div>
      <label>
        <input type="checkbox" onChange={this.handleValuesChange} checked={this.state.checked}/> email
      </label>
    </div>;
  }
}

from mantra.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.