DEV Community

loading...
Cover image for a first look at redwoodJS: part 1 - setup

a first look at redwoodJS: part 1 - setup

ajcwebdev profile image anthonyCampolo Updated on ・6 min read

I like to think that most things can be achieved. Whatever you have in your head you can probably pull off with code as long as it's possible within the constraints of the universe.

It's just a matter of time... and money... and attention.

Tom Preston-Werner - Full Stack Radio

RedwoodJS is a fullstack, serverless framework for the Jamstack. I will start at the very beginning and assume no prior knowledge of Redwood although I do assume a basic knowledge of React. But I'm talking really basic, you'll be fine if you:

  • Know what a component is
  • Have written at least a dozen lines of JSX
  • Have generated at least one project with create-react-app

If none of that made sense you should click the link to the create-react-app docs and work through those before reading this. This series is geared towards someone who has at least a few months experience, around the point where they start getting comfortable with the workflows of git, npm/yarn, and the terminal.

You will need yarn for this tutorial which has slight differences from npm. You can find installation instructions here or just enter npm install -g yarn.

1.1 yarn create redwood-app

The first step is to create our Redwood project.

yarn create redwood-app ./ajcwebdev-redwood
Enter fullscreen mode Exit fullscreen mode

This will create a folder called ajcwebdev-redwood that holds all the code that will be generated. You can call your project anything you want, just make sure to keep using your name anytime I use ajcwebdev-redwood in a terminal command.

01-yarn-create-redwood-app

There is also a handy-dandy guide to a list of fantastic community resources. Come hang out with us, we're super fun!

02-join-the-community

1.2 yarn redwood dev

yarn rw is the same as yarn redwood and can be used to save a few keystrokes. cd into the new folder and initialize a git repository. Before entering these commands I created an empty repository on my GitHub.

cd ajcwebdev-redwood
git init
git add .
git commit -m "Nailed it"
git branch -M main
git remote add origin https://github.com/ajcwebdev/ajcwebdev-redwood.git
git push -u origin main
Enter fullscreen mode Exit fullscreen mode

Start the development server.

yarn rw dev
Enter fullscreen mode Exit fullscreen mode

03-yarn-redwood-dev

Our server is now running on localhost:8910 (to remember just count 8-9-10). Open a browser and enter localhost:8910 into the address bar. If you have done everything correctly up to this point you will see the Redwood starter page.

04-redwood-starter-page

WHOOPS, it worked, we're up and running. Don't worry too much about what it says about custom routes, we'll talk about that in the next article. Here is the file structure that has been created for us.

├── api
│   ├── prisma
│   │   ├── schema.prisma
│   │   └── seeds.js
│   └── src
│       ├── functions
│       │   └── graphql.js
│       ├── graphql
│       ├── lib
│       │   └── db.js
│       └── services
└── web
    ├── public
    │   ├── favicon.png
    │   ├── README.md
    │   └── robots.txt
    └── src
        ├── components
        ├── layouts
        ├── pages
        │   ├── FatalErrorPage
        │   │   └── FatalErrorPage.js
        │   └── NotFoundPage
        │       └── NotFoundPage.js
        ├── index.css
        ├── index.html
        ├── index.js
        └── Routes.js
Enter fullscreen mode Exit fullscreen mode

In Redwood our frontend code is contained in the web folder and our backend code is contained in the api folder. For the purpose of this article we'll only be working in the web folder. Redwood structures the web folder a bit like create-react-app projects with a public and src folder.

1.3 redwood generate page

With our application now set up we can start creating pages. We'll use the generate page command to create a home page and a folder to hold that page.

yarn rw g page home /
Enter fullscreen mode Exit fullscreen mode

The generate page command creates a couple of extra files that will be useful later in the series. These include a Storybook file along with testing and mocking files.

09-yarn-redwood-generate-home

Since I only entered home it will use that to name both the folder and the component file but you can specify each if necessary.

10-web-src-pages

Return to your browser and you will now see a new page instead of the landing page.

11-HomePage

Let's look at the code that was generated for this page. It's a component called HomePage that returns a <div> with a header <h1> and a paragraph tag <p>.

// web/src/pages/HomePage/HomePage.js

import { Link, routes } from '@redwoodjs/router'

const HomePage = () => {
  return (
    <>
      <h1>HomePage</h1>
      <p>
        Find me in <code>./web/src/pages/HomePage/HomePage.js</code>
      </p>
      <p>
        My default route is named <code>home</code>, link to me with `
        <Link to={routes.home()}>Home</Link>`
      </p>
    </>
  )
}

export default HomePage
Enter fullscreen mode Exit fullscreen mode

This should be pretty self-explanatory if you have experience with React. If this doesn't look familiar it would be helpful to spend a little time studying React by itself before jumping into Redwood.

Now we'll edit the page and see what happens.

// web/src/pages/HomePage/HomePage.js

const HomePage = () => {
  return (
    <>
      <h1>ajcwebdev</h1>
      <p><a href="https://dev.to/ajcwebdev">Blog</a></p>
      <p><a href="https://twitter.com/ajcwebdev">Twitter</a></p>
      <p><a href="https://github.com/ajcwebdev">GitHub</a></p>
    </>
  )
}

export default HomePage
Enter fullscreen mode Exit fullscreen mode

Feel free to include links to your own social accounts. With those changes made return to your browser.

12-HomePage-edit

Now we are going to generate our about page.

yarn rw g page about
Enter fullscreen mode Exit fullscreen mode

Like before we created an AboutPage component inside of an AboutPage folder along with a few additional files.

13-yarn-redwood-generate-about

We don't yet have a link to get to it from the home page, so for now you'll need to enter the route manually into your browser address bar by adding /about after localhost:8910.

14-AboutPage

Open up the code and it's another React component much like the last! Components are kind of a big deal in React.

import { Link, routes } from '@redwoodjs/router'

const AboutPage = () => {
  return (
    <>
      <h1>AboutPage</h1>
      <p>
        Find me in <code>./web/src/pages/AboutPage/AboutPage.js</code>
      </p>
      <p>
        My default route is named <code>about</code>, link to me with `
        <Link to={routes.about()}>About</Link>`
      </p>
    </>
  )
}

export default AboutPage
Enter fullscreen mode Exit fullscreen mode

We can also edit this page just like the home page.

const AboutPage = () => {
  return (
    <>
      <h1>About</h1>
      <p>Full stack web developer</p>
    </>
  )
}

export default AboutPage
Enter fullscreen mode Exit fullscreen mode

With those changes return to your browser.

15-AboutPage-edit

We can also see these files have been added to our src folder.

16-project-structure-web-home-about

In the next part we'll take a look at Redwood's router and create links for the pages we created.

Discussion

pic
Editor guide