Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <div id="app"></div>
              
            
!

CSS

              
                
              
            
!

JS

              
                const { createStore, applyMiddleware, bindActionCreators } = Redux;
const { Provider, connect } = ReactRedux;
const ReduxThunk = ReduxThunk.default; 

class App extends React.Component { 
  componentDidMount = () => {
    this.props.fetchUsers();
  }
  
  handleChange = (event) => {
    this.props.selectUser(event.target.value);
  }
  
  render() {
    const userList = this.props.userList;
    const user = this.props.currentUser;
    
    if (this.props.loading) {
      return (
        <div>Loading...</div> 
      )
    } else if (this.props.error) {
      return (
        <div>{this.props.error.message}</div>
      )
    }
    
    return (
      <div> 
        <Select onChange={this.handleChange} userList={this.props.userList} />
        <CurrentUser currentUser={this.props.currentUser} />
      </div>
    );
  }
}

const Select = (props) => {
  renderOptions = () => (
    props.userList.map(user => 
      <option value={user.id} key={`user-${user.id}`}>{user.name}</option>
    )
  )
  
  return (
    <select onChange={props.onChange} defaultValue="">
      <option value="">Select an avatar</option>
      {this.renderOptions()}
    </select>
  ); 
}

const CurrentUser = (props) => {
  return (
    <div> 
      <p>{props.currentUser && JSON.stringify(props.currentUser)}</p>
    </div>
  )  
} 

/* --- REDUCERS --- */
function reducer(state, action) {
  switch (action.type) {
    case "REQUEST_USERS":
      return {
        ...state,
        loading: action.loading,
        userList: []
      }
    case "REQUEST_USERS_SUCCESS":
      return {
        ...state,
        loading: action.loading,
        userList: action.userList
      }
    case "REQUEST_USERS_ERROR":
      return {
        ...state,
        loading: action.loading,
        error: action.error
      }
    case "SELECT_USER":
      return {
        ...state,
        currentUser: state.userList.find(user => user.id == action.currentUser)
      }
    default:
      return state
  }
};

/* --- ACTION CREATORS --- */
const actionCreators = {
  requestUsers: () => {
    return {
      type: "REQUEST_USERS",
      loading: true,
      error: null
    }
  },
  requestUsersSuccess: (userList) => {
    return {
      type: "REQUEST_USERS_SUCCESS",
      userList,
      loading: false,
      error: null
    }
  },
  requestUsersError: (error) => {
    return {
      type: "REQUEST_USERS_ERROR",
      loading: false,
      userList: [],
      error
    }
  },
  fetchUsers: () => {
    return function (dispatch) {
      dispatch(actionCreators.requestUsers());
      
      return fetch("https://jsonplaceholder.typicode.com/users")
      .then(
        response => {
          if (response.ok) {
            return response.json()
          }
          throw new Error("404");
        }
      )
      .then(json =>
        dispatch(actionCreators.requestUsersSuccess(json))
      ).catch(error => {
        console.error(error);
        dispatch(actionCreators.requestUsersError(error))
      })
    }
  },
  selectUser: (currentUser) => {
    return {
      type: "SELECT_USER",
      currentUser
    }
  }
};

/* --- STORE --- */
const preloadedState = {userList: [], currentUser: null};
const store = createStore(reducer, preloadedState, applyMiddleware(ReduxThunk));

/* --- CONNECT --- */
const AppContainer = connect(
  function mapStateToProps(state, props) {
		return {
      userList: state.userList,
			currentUser: state.currentUser,
      loading: state.loading,
      error: state.error
		};
	},
  function mapDispatchToProps(dispatch) {
		return bindActionCreators(actionCreators, dispatch);
	}
)(App);

/* --- RENDER THE APP --- */
ReactDOM.render(
  <Provider store={store}>
    <AppContainer />
  </Provider>,
  document.getElementById("app")
);
              
            
!
999px

Console