Skip to main content

Dead Simple React.js with Meteor

I spent a little time exploring the patterns involved in using React.js with Meteor. It's incredibly easy, it turns out. I'll show some examples here.

The setup:

meteor add kadira:flow-router
npm install react react-dom react-mounter
npm install react-addons-pure-render-mixin
meteor add react-meteor-data

Then of course remove all blaze related meteor packages. Ok, Some basic component patterns: Let's create one that accepts a single argument:

Hello.jsx

import React from 'react';
export const Hello = ({name}) => (
  <div>Hello, {name}</div>
);

That's all there is to it. Now, let's see a pattern for a component that takes two arguments. We can see that to add further arguments, we can just tack them on after the first two:

TwoArgs.jsx

import React from 'react';
export const TwoArgs = ({one, two}) => (
  <div>
    <h2>TwoArgs!</h2>
    <h3>One is: {one}</h3>
    <h3>Two is: {two}</h3>
  </div>
);

"But, I really want a button that sets some text!"  Alright then, glad you asked:

WithButton.jsx

import React from 'react';

const handleClick = (event) => {
  event.preventDefault();
  console.log('clicked');
  document.querySelector('#foo').innerHTML="Yeah, I clicked it";
};

const WithButton = () => (
  <div>
    <button onclick="handleClick">Click Me&lt/button>
    <p>A handsome button sits above me</div>
    <p id="foo"></>
  </div>
);


Now, lets make a layout we can render our components into:

Layout.jsx

import React, { Component } from 'react';

export const Layout = ({content}) => (
  <div>{content}</div>
);

Finally, let's set up some routing:

router.js

import React from 'react';
import { mount } from 'react-mounter';
import { Layout } from './Layout.jsx';
import { Hello } from './Hello.jsx';
import { TwoArgs } from './TwoArgs.jsx';
import { WithButton } from './WithButton.jsx';

FlowRouter.route("/", {
  action() {
   mount(Layout, {
     content: ()
   });
  }
});

FlowRouter.route("/momo", {
  action() {
   mount(Layout, {
     content: ()
   });
  }
});

FlowRouter.route("/buttn", {
  action() {
   mount(Layout, {
     content: ()
   });
  }
});

Comments

Popular posts from this blog

Passing Functions and Lambdas into Functions with Ruby

Ruby's New Style of Lambda Functions f = ->( m ) { p m } f.call( 1 ) #=> 1 Which of course means the same thing as: f = lambda { |n| p n } f.call(1) #=> 1 Ruby Proc Objects p = Proc.new { |n| p n + 2 } p.call(2) #=> 4 Using a Function as a Closure in Ruby def domo( k ) ->(m) { p m + k } end z = domo( 5 ) z.call( 5 ) #=> 10 Function :domo takes a single parameter. Within :domo , we create a lambda that takes a single parameter, and adds that parameter to the value :domo takes in as its parameter. Then, we assign z to be the result of the lambda in :domo with its 'k' parameter loaded with 5. When z is called, we pass (another) 5 to it. This parameter loads the lambdas n parameter. The lambda executes, essentially adding n (5) + k (5) and yielding the result of 10. The thing about closures such as this is, we can load the initial value of the lambda to be whatever we want it to be when assigning the function :domo '...

Fun with Meteor, React, and React-Bootstrap

React-Bootstrap is pretty cool. I decided to play with it a bit.  Here are the basics. In an already set up Meteor project (set up for React), it is added thus: npm install --save react-bootstrap Once this is done, you also need to add a bootstrap library. It could either be the twitter bootstrap meteor package, or you can link to it. For the purpose of my demo, I just grabbed a couple links from the React-Bootstrap site that they had handy for pulling in from a CMS: index.html <head> <!-- Latest compiled and minified CSS --> <link href="https://maxcdn.bootstrapcdn.com/bootstrap/latest/css/bootstrap.min.css" rel="stylesheet"> </link> <!-- Optional theme --> <link href="https://maxcdn.bootstrapcdn.com/bootstrap/latest/css/bootstrap-theme.min.css" rel="stylesheet"> </link> </head> Now, let's make a layout, and then create a component tha...