Giter VIP home page Giter VIP logo

react-mobx-realworld-example-app's Introduction

React + Mobx Example App

React + Mobx codebase containing real world examples (CRUD, auth, advanced patterns, etc) that adheres to the RealWorld spec and API.

Originally created for this GH issue. The codebase is now feature complete; please submit bug fixes via pull requests & feedback via issues.

We're currently working on some docs for the codebase (explaining where functionality is located, how it works, etc) but most things should be self explanatory if you have a minimal understanding of React/Mobx.

Getting started

You can view a live demo over at https://react-mobx.realworld.io/

To get the frontend running locally:

  • Clone this repo
  • npm install to install all req'd dependencies
  • npm start to start the local server (this project uses create-react-app)

Making requests to the backend API

For convenience, we have a live API server running at https://conduit.productionready.io/api for the application to make requests against. You can view the API spec here which contains all routes & responses for the server.

The source code for the backend server (available for Node, Rails and Django) can be found in the main RealWorld repo.

If you want to change the API URL to a local server, simply edit src/agent.js and change API_ROOT to the local server's URL (i.e. localhost:3000/api)

Functionality overview

The example application is a social blogging site (i.e. a Medium.com clone) called "Conduit". It uses a custom API for all requests, including authentication. You can view a live demo over at https://react-mobx.realworld.io/

General functionality:

  • Authenticate users via JWT (login/signup pages + logout button on settings page)
  • CRU* users (sign up & settings page - no deleting required)
  • CRUD Articles
  • CR*D Comments on articles (no updating required)
  • GET and display paginated lists of articles
  • Favorite articles
  • Follow other users

The general page breakdown looks like this:

  • Home page (URL: /#/ )
    • List of tags
    • List of articles pulled from either Feed, Global, or by Tag
    • Pagination for list of articles
  • Sign in/Sign up pages (URL: /#/login, /#/register )
    • Use JWT (store the token in localStorage)
  • Settings page (URL: /#/settings )
  • Editor page to create/edit articles (URL: /#/editor, /#/editor/article-slug-here )
  • Article page (URL: /#/article/article-slug-here )
    • Delete article button (only shown to article's author)
    • Render markdown from server client side
    • Comments section at bottom of page
    • Delete comment button (only shown to comment's author)
  • Profile page (URL: /#/@username, /#/@username/favorites )
    • Show basic user info
    • List of articles populated from author's created articles or author's favorited articles

Brought to you by Thinkster

react-mobx-realworld-example-app's People

Contributors

andykog avatar apai4 avatar bluepeter avatar ericsimons avatar hhsadiq avatar lelush-uno avatar o4epegb avatar robbiedhickey avatar supermanever avatar vasek avatar vkarpov15 avatar xuchaobei avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

react-mobx-realworld-example-app's Issues

Project is broken by default for new installs due to .env dependency

We have a dependency on the custom-react-scripts package which requires the user to have a .env file present at the root of their project directory. If the .env file is not present, then subsequent npm run start or npm run build commands will fail. Right now, the only customization we require is to enable support for decorators in the .env file i.e. REACT_APP_DECORATORS=true. A couple potential solutions:

  1. Remove .env from .gitignore so it 'just works'
  2. Add documentation to README explaining the dependency
  3. Add preinstall script to generate the file the first time the user runs an npm install
  4. Update to latest version of create-react-app, eject, and make decorator support the default

@andykog Thoughts?

Approaches 1-3 feel like band-aids. Approach 4 initially struck me as overkill but might have some merit. The reason: I leveraged custom-react-scripts to get up and running quickly but the project has not been updated in some time. This may present an opportunity to remove an unnecessary dependency on a package that may no longer actively be maintained.

`clear` method should probably be an action?

Thanks for having this repo as a nice example of MobX + React. I was just browsing through the code and noticed that this method should probably be an action unless I'm missing something? Of course, it probably will work just fine unless you have it set to strict mode, but I figured I'd point it out since pretty much all your other methods across the stores are correctly decorated as actions when they mutate state.

Cheers!

API calls in componentWillMount vs componentDidMount

Is there any reason API calls are made in WillMount as opposed to DidMount

If I'm reading this correctly the React docs say use DidMount:

"componentDidMount() is invoked immediately after a component is mounted. Initialization that requires DOM nodes should go here. If you need to load data from a remote endpoint, this is a good place to instantiate the network request. Setting state in this method will trigger a re-rendering."
https://facebook.github.io/react/docs/react-component.html

https://daveceddia.com/where-fetch-data-componentwillmount-vs-componentdidmount/

Typescript typed issue on Stores : Type '{}' is not assignable to type 'Readonly<Props>

Thanks for the repo.

I am confused on how to pass the props typed ( React.Component <Props, State> ) for each components. Since I have defined the props of App, I get issue on index.tsx,

Type '{}' is not assignable to type 'IntrinsicAttributes & IntrinsicClassAttributes<App> & Readonly<{ children?: ReactNode; }> & Reado...'.
  Type '{}' is not assignable to type 'Readonly<Props>'.
    Property 'commonStore' is missing in type '{}'.

Please help on this.

Index.tsx

import * as React from 'react';
import * as ReactDOM from 'react-dom';
import App from './App';
import registerServiceWorker from './registerServiceWorker';
import 'bootstrap/dist/css/bootstrap.css';
import './index.css';
import { Provider } from 'mobx-react';
import { useStrict } from 'mobx';
import { HashRouter } from 'react-router-dom';
import commonStore from './Stores/CommonStore';

// import ReconnectingWebsocket from 'reconnecting-websocket';

const stores = {
  commonStore
};

useStrict(true);

const root = (
  <Provider {...stores}>
    <HashRouter>
      <App />
    </HashRouter>
  </Provider>
);

ReactDOM.render(
  root,
  document.getElementById('root') as HTMLElement
);
registerServiceWorker();

App.tsx


import './App.css';
import * as React from 'react';
import Header from './Layouts/Utils/Header/Header';
import { observer, inject } from 'mobx-react';
import SingleDashboard from './Layouts/Pages/Dashboard/SingleDashboard/SingleDashboard';
import { withRouter } from 'react-router-dom';
import { commonStoreType } from './Stores/CommonStore';

interface Props {
  commonStore: commonStoreType;
}

interface State {

}

@inject('commonStore')
@withRouter
@observer
export default class App extends React.Component <Props, State> {
  constructor(props: Props) {
    super(props);
    // tslint:disable-next-line:no-console
    console.log(props);
  }

  componentWillMount() {
    if (!this.props.commonStore.token) {
      this.props.commonStore.setAppLoaded();
    }
  }

  render () {
    if (this.props.commonStore.appLoaded) {
      return (
        <div>
          <Header />
          <SingleDashboard />
        </div>
      );
    }
    return(
      <h1> Login </h1>
    );
    
  }
}

CommonStore


import { observable, action, reaction } from 'mobx';

class CommonStore  {

  @observable appName = 'Conduit';
  @observable token: string | null |undefined = window.localStorage.getItem('jwt');
  @observable appLoaded = false;

  constructor() {
    
  }

 
}

const commonStore = new CommonStore();
export type commonStoreType = typeof commonStore;
export default commonStore;

Editor doesn't properly clear state after leaving edit article view

It seems like there's a bug where the Editor view doesn't properly clear it's state. If you begin editing an article, leave the page, and then try to create a new post any time after it won't reset the editor store.

I'd be happy to look into this and submit a PR if this is something that needs addressing.

GH pages realworld domain

@hodorswit just FYI, I set up the domain https://react-mobx.realworld.io to point at the GH page for this repo! Looks like you currently have links/assets pointing to the absolute URL on GH pages itself though, so the app is currently breaking under the new URL.

Editor not loading article content when run locally

Is the example app running the latest version of the code? When running the app locally the /editor/:slug view doesn't seem to reload the article's content for me when run locally.

I did some investigating and I think this might due to the ordering of the Editor paths. I've created a PR that should resolve this, although I think further improvements could be made to the Editor view.

Safety for password

Is it safe to put the password in the observable in the AuthStore? Is it a good practice? If not, what should we do?

Use pure components there possible

Hi, there are quite few places there PureComponent or React.memo could be used to reduce rerender.

For example https://github.com/gothinkster/react-mobx-realworld-example-app/blob/master/src/components/ListPagination.js#L3 . ListPagination is rendered 3 times on each page change, if I wrap it in React.memo it is rendered only one time as it should be.

Other components which cause unneeded rerender EditProfileSettings, FollowUserButton, Banner, YourFeedTab, GlobalFeedTab, TagFilterTab.

Thank you

This is a much needed resource. Really excellent, thanks!

Sass files

Hello author,

Could you pls tell me how to use sass with this starter kit?
I saw your main.css from the CDN but idk how to setup sass/images folder for my proj 💃
thanks in advance ;)

Passing data between stores

I have a noob question to ask about passing data between stores, which I didn't understand how it works.

authStore accesses data from userStore and commonStore and it just basically imports them from their singleton export. however all these stores are also imported to index.js and passed to provider of mobx in order to inject and use them in react components. What I don't get here is that these should be different instances of userStore and commonStore, shouldn't they? Because they are imported twice to different js files.

I'm trying to do the same pattern in my very basic app where I need to pass data between stores, but it just doesn't work out. when I change a data in a store which I access from within a React component, it doesn't affect the same store which is imported to another store to pass its data, because they are different instances of the store classes.

I hope I made my confusion clear here, and I really would appreciate some help to understand some fundamentals here, and the point I'm missing.

Thanks a lot!

How do you inport the css files?

After cloning the project,I try to understand the logic and the code.But I wonder where are the CSS files and how do you set the style?Could you pleae tell me?By the way,thanks a lot for your contribution and share.

Articles.byAuthor functions incorrect signature

byAuthor: (author, page, query) => requests.get(/articles?author=${encode(author)}&${limit(5, page)}),

query is declared by not used.

articlesStore.js calls byAuthor with author, page, LIMIT, where LIMIT is not used at all.

Clicking on a 2nd tag does not load that tag's content

The demo you have running works perfectly. However, when I run this locally, everything work fine, except that swapping to a new tag does not work. The initial click of a tag will display. But then trying to swap to a new tag will not load the new tag contents.

Upgrade to react-router 4?

Are there any plans to upgrade this to react-router 4? It's markedly different, and seems like it would be a good evolution for this project.

request for test samples

it would be nice to include a few samples for testing with jest. like tests for api requests and constructing snapshot tests (handling the mobx store). thanks. this is a great resource.

(edit) I did find some additional egghead.io videos which appear to include testing material and will look at those in the meantime. https://egghead.io/browse/libraries/mobx

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.