All Projects → yildizberkay → Redux Example

yildizberkay / Redux Example

Licence: mit
[React 15.6 and Redux 3.7] Sample redux project which is prepared using 500px API

Programming Languages

javascript
184084 projects - #8 most used programming language
es6
455 projects

Projects that are alternatives of or similar to Redux Example

Eslint Formatter Github
See ESLint errors and warnings directly in pull requests.
Stars: ✭ 63 (-27.59%)
Mutual labels:  eslint
Cordovue
A sample Apache Cordova application using VueJS.
Stars: ✭ 66 (-24.14%)
Mutual labels:  eslint
Threejs Es6 Webpack Starter
Three.js ES6 starter project with a sane webpack configuration
Stars: ✭ 85 (-2.3%)
Mutual labels:  eslint
Astexplorer.app
https://astexplorer.net with ES Modules support and Hot Reloading
Stars: ✭ 65 (-25.29%)
Mutual labels:  eslint
Lint Staged
🚫💩 — Run linters on git staged files
Stars: ✭ 9,492 (+10810.34%)
Mutual labels:  eslint
Gatsby Themes
Get high-quality and customizable Gatsby themes to quickly bootstrap your website! Choose from many professionally created and impressive designs with a wide variety of features and customization options.
Stars: ✭ 1,208 (+1288.51%)
Mutual labels:  eslint
Next React Graphql Apollo boostrap
React + GraphQL + Next.js project architecture that I play with right now
Stars: ✭ 59 (-32.18%)
Mutual labels:  eslint
Gitbook Starter Kit
GitBook Starter Kit.
Stars: ✭ 86 (-1.15%)
Mutual labels:  eslint
Vue Web Extension
🛠️ A Vue CLI 3+ preset (previously a Vue CLI 2 boilerplate) for quickly starting a web extension with Vue, Babel, ESLint and more!
Stars: ✭ 1,147 (+1218.39%)
Mutual labels:  eslint
Sapper Typescript Graphql Template
A template that includes Sapper for Svelte, TypeScript preprocessing, and a GraphQL server through TypeGraphQL
Stars: ✭ 84 (-3.45%)
Mutual labels:  eslint
Starter React Flux
Generate your React PWA project with TypeScript or JavaScript
Stars: ✭ 65 (-25.29%)
Mutual labels:  eslint
Eslint Plugin Vuejs Accessibility
An eslint plugin for checking Vue.js files for accessibility
Stars: ✭ 66 (-24.14%)
Mutual labels:  eslint
Mostly
They mostly come at night; mostly.
Stars: ✭ 78 (-10.34%)
Mutual labels:  eslint
Sowing Machine
🌱A React UI toolchain & JSX alternative
Stars: ✭ 64 (-26.44%)
Mutual labels:  eslint
Svelte Example
🚀 📚 Some examples to test the Svelte Framework
Stars: ✭ 85 (-2.3%)
Mutual labels:  eslint
Vue Music
使用vue2.0构建音乐播放器
Stars: ✭ 60 (-31.03%)
Mutual labels:  eslint
Vue Admin Element
(Vue2 演示项目)物业后台管理系统 - ElementUI ( 基本结构已完成, 剩下的就是具体业务开发; 如有疑问请留言 )
Stars: ✭ 73 (-16.09%)
Mutual labels:  eslint
Eslint Plugin Github
An opinionated collection of ESLint rules used by GitHub.
Stars: ✭ 86 (-1.15%)
Mutual labels:  eslint
React Redux Saucepan
A minimal and universal react redux starter project. With hot reloading, linting and server-side rendering
Stars: ✭ 86 (-1.15%)
Mutual labels:  eslint
Eslint Config Standard Jsx
ESLint Shareable Config for JSX support in JavaScript Standard Style
Stars: ✭ 79 (-9.2%)
Mutual labels:  eslint

Redux Sample Project with 500px API

This repository was prepared to make a simple introduction to Redux. The project was developed using ES6 and Webpack, both ES6 and Webpack are prerequisite.

Codacy Badge

Demo GIF

Table of contents

Installation

Clone the repository and install dependencies.

git clone https://github.com/yildizberkay/redux-example.git
cd redux-example
npm install
npm start
open http://localhost:3000

ESLint

npm run lint

Why do we need Redux?

Let's think about an ordinary React app. There are components, states, props and API actions. A component calls another components and if you trigger an UI state, view is updated and etc... Finally, the app becomes complex and you cannot control it.

React is a JavaScript library for creating user interfaces by Facebook and Instagram. Many people choose to think of React as the V in MVC.

Source: https://facebook.github.io/react/docs/why-react.html

We need a state container like Redux. How does Redux solve this problem? Basically, there is a single store source which is read-only. If you want to make an action like load photos from API, you have to make this using actions and reducers.

There are 3 primary parts of Redux: actions, reducers and store.

Actions

Actions are used to get data. API requests can be made in this part.

Actions are payloads of information that send data from your application to your store. They are the only source of information for the store.

Source: https://redux.js.org/docs/basics/Actions.html

In the following sample, there is an action that is named as searchPhotoAction. This action returns a function that contains searchWithPhotoAPI and dispatch function notifies store. As you can see, first of all, dispatch sends a "SEARCH_PENDING" type, this shows a spinner in the screen. After that, if the request is succeeded, dispatch sends a "SEARCH_DONE", photo array and another info.

function searchWithPhotoAPI(keyword, page, dispatch) {
  if (page >= 2) {
    dispatch({
      type: types.SEARCH_PENDING_FOR_NEXT,
    });
  } else {
    dispatch({
      type: types.SEARCH_PENDING,
    });
  }
  photoSearch(keyword, page, (data) => {
    dispatch({
      type: types.SEARCH_DONE,
      photos: data.photos,
      page,
      keyword,
    });
  });
}

export function searchPhotoAction(keyword, page = 1) {
  return (dispatch) => {
    searchWithPhotoAPI(keyword, page, dispatch);
  };
}

Reducers

Data comes to Reducers, and it is reshaped here. After that, it is passed to views. You can combine previous state with next state in this step.

In following code, returned state is changing according to action.type. If action.type is types.SEARCH_PENDING, we will show a spinner in view.

const searchPhotos = (state = initialState, action) => {
  switch (action.type) {
    case types.SEARCH_DONE:
      return {
        ...state,
        photos: [...state.photos, ...action.photos],
        status: 'DONE',
        page: action.page,
        keyword: action.keyword,
      };
    case types.SEARCH_PENDING_FOR_NEXT:
      return {
        ...state,
        status: 'PENDING_FOR_NEXT',
      };
    case types.SEARCH_PENDING:
      return {
        ...state,
        photos: [],
        status: 'PENDING',
      };
    default:
      return state;
  }
}

Store

In the Redux, there is only one store. It can be created directly using createStore as well as applyMiddleware.

// containers/App.jsx

const reducer = combineReducers({ searchPhotos });
const store = createStore(reducer, {}, applyMiddleware(thunk));

const App = () => (
  <div>
    <Provider store={store}>
      <SearchApp />
    </Provider>
  </div>
)

Calling Actions

Before call an action, we have to combine actions and dispatch function. connect([mapStateToProps], [mapDispatchToProps], [mergeProps], [options]) function connects a React component to a Redux store.

bindActionCreators(actionCreators, dispatch) combines actions and dispatch function.

// containers/SearchApp.jsx

...

class SearchApp extends PureComponent {
  render() {
    const actions = bindActionCreators(action, this.props.dispatch);
    return (
      <div>
        <div id="header" className="header">
          <div className="container">
            <div className="row">
              <div className="col-md-6 col-md-offset-3 search-bar-content">
                <h1>Search on 500px</h1>
                <SearchInput actions={actions} status={this.props.status} />
              </div>
            </div>
          </div>
        </div>
        <div className="container">
          <PhotoList actions={actions} photos={this.props.photos} status={this.props.status} />
        </div>
      </div>
    );
  }
}

...

export default connect(
  mapStateToProps,
)(SearchApp);

Now, you can call actions via following code.

actions.searchPhotoAction(keyword, page)

Directory structure

Root
├── public
│   ├── actions
├── src
│   ├── actions
│   │   └──
│   ├── api
│   │   └──
│   ├── components
│   │   └──
│   ├── constants
│   │   └──
│   ├── containers
│   │   └──
│   ├── reducers
│   │   └──
│   ├── stylesheets
│   │   └──
│   └── index.js
├── .eslintignore
├── .eslintrc.json
├── package.json
└── README.md

Resources

Changelog

Nov 21, 2017

  • README update.
  • ReactJS version update. [15.6.2]
  • Redux version update. [3.7.2]
  • React Scripts migration.

Aug 10, 2017

  • Dependencies of the project are updated.
  • Code refactoring.

Oct 25, 2015

  • Fetch support for all browsers. @pcanterini PR
  • Airbnb ESLint rules were added.

Oct 12, 2015

  • Initial commit.
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].