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...

OS X Process-Fu 101

OS X is of course a variety of BSD Unix, not Linux. Often is the case where Linux commands either don't work, or don't give the same results on OS X as they do on Linux. For example, a basic netstat will give you a long list of the processes making network connections on your box. On Linux, various command line options can help you drill down from there, to get the answers to more specific queries. On OS X, those same queries don't give you the answers you're seeking. This isn't a compare and contrast article between Linux and OSX. Instead, I'm just noting various commands that can give answers to some basic questions. On OS X, for example, to get an answer to the question: "What process is running on port 50224?" we would use: sudo lsof -Pn | grep 50224 This will give you a list of everything connected to port 50224, sans any kind of headings. A much more abbreviated command that will give you essentially the same things, but with column...