Giter Site home page Giter Site logo

react-cookbook's Introduction

React-Cookbook

Recipes for making your React.js Components Awesome, or at least a little cleaner

Basics

Use 'className' for class

render: function() {
  return (
    <button className="Btn Primary">Button</button>
  );
}

Style tags can be done inline

NOTE: React uses camelCased for attribute names 'backgroundColor for 'background-color'

render: function() {
  return (
    <div style={{color: 'white', backgroundColor: 'lightblue'}}>Test</div>
  );
}

view fiddle

Intermediate

Use ternary operator for if/else

render: function() {
  var coinflip = Math.random() < .5;
  return (
    <div>
      {(coinflip) ?
        <div>Heads</div> :
        <div>Tails</div>
      }
    </div>
  );
}

view fiddle

Use 'map' to cleanly iterate over arrays

render: function() {
  var list = ['one', 'two', 'three'];
  return (
    <ul>
      {list.map(function(item) {
        return <li>{item}</li>
      })}
    </ul>
  );
}

view fiddle

Use 'classSet' to toggle classes

render: function() {
  var cx = React.addons.classSet;
  return <div className={cx({
    'message': true,
    'message-important': this.props.isImportant,
    'message-read': this.props.isRead
  })}>Great, I'll be there.</div>;
}

view fiddle

ReactLink can be used for two-way data bindings

mixins: [React.addons.LinkedStateMixin],
getInitialState: function() {
  return {value: 'Hello!'};
},
render: function() {
  return (
    <div>
      <input type="text" valueLink={this.linkState('value')} />
      <div>You typed: {this.state.value}</div>
    </div>
  );
}

view fiddle

Reduce can be used for simple list filtering

return (
  <ul>
      {messages.reduce(function(messages, item) {
        if (match(item, filter)) {
          messages.push(<li>{item.title}</li>)
        }
        return messages;
      }, [])}
  </ul>
);

view fiddle

Use bind to map specifiic parameters for event handlers

		onClick: function(value, e) {
			e.preventDefault();
			this.props.onChange({
				choice: value
			}, true);
			this.setState({
				choice: value
			});
		},

		render: function() {
			var self = this,
				cx = React.addons.classSet,
				choices = this.props.choices,
				selected = this.state.choice;

			return (
				<ul className="Choices">
					{choices.map(function(choice) {
						return <li key={choice.value} className={cx({'Selected': (choice.value === selected)})}><a href='#' onClick={self.onClick.bind(null, choice.value)}>{choice.label}</a></li>;
					})}
				</ul>
			);
		}

Note: bind should have "null" as its first parameter, React will automatically re-bing with "this"

view fiddle

Handler callback functions can be used a props to get events for subcomponents

		onClick: function(value, e) {
			e.preventDefault();
			this.props.onChange({
				choice: value
			}, true);
			this.setState({
				choice: value
			});
		},


...

    <div>
        <div>{'Selected: ' + this.state.selected}</div>
        <Chooser choices={choices} onChange={this.choiceChange} />
    </div>

view fiddle

react-cookbook's People

Contributors

carystanley avatar

Watchers

 avatar  avatar

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.