React Base Fiddle (JSX)
Starting point for creating JSFiddles with React. This uses React with Addons.
by jeffbski
HTML
<script src="https://cdnjs.cloudflare.com/ajax/libs/babel-core/5.8.24/browser.js"></script>
<script src="https://npmcdn.com/react@latest/dist/react-with-addons.js"></script>
<script src="https://npmcdn.com/react-dom@latest/dist/react-dom.js"></script>
<script src="https://npmcdn.com/redux@^3.5.2/dist/redux.min.js"></script>
<script src="https://npmcdn.com/react-redux@^4.4.5/dist/react-redux.min.js"></script>
<script src="https://unpkg.com/@reactivex/[email protected]/dist/global/Rx.js"></script>
<script src="https://unpkg.com/[email protected]/dist/redux-logic.min.js"></script>
<script src="https://facebook.github.io/react/js/jsfiddle-integration-babel.js"></script>
<div id="appContainer">
<!-- This element's contents will be replaced with your component. -->
</div>
JavaScript 1.7
const { createStore, applyMiddleware } = Redux;
const { createLogic, createLogicMiddleware } = ReduxLogic;
const { connect, Provider } = ReactRedux;
const { ajax } = Rx.Observable;
const initialState = {
list: [],
fetchStatus: ''
};
const NPM_SEARCH = 'NPM_SEARCH';
const NPM_SEARCH_FULFILLED = 'NPM_SEARCH_FULFILLED';
const NPM_SEARCH_REJECTED = 'NPM_SEARCH_REJECTED';
function npmSearch(ev) {
return { type: NPM_SEARCH, payload: ev.target.value };
}
const npmSearchLogic = createLogic({
type: NPM_SEARCH,
debounce: 500, // ms
latest: true, // take latest only
validate({ getState, action }, allow, reject) {
if (action.payload) {
allow(action);
} else { // empty request, silently reject
reject();
}
},
// use the resolved/errored values from the returned obs
// and apply these action types
processOptions: {
dispatchReturn: true,
successType: NPM_SEARCH_FULFILLED,
failType: NPM_SEARCH_REJECTED
},
// use rxjs ajax injected as httpClient from configureStore logic deps
process({ httpClient, getState, action }) {
// dispatch the resolved or error values from the returned obs
return httpClient({
url: `https://npmsearch.com/query?q=${action.payload}&fields=name,description`,
crossDomain: true,
responseType: 'json'
})
.map(ret => ret.response.results); // use results prop of payload
}
});
const deps = { // injected dependencies for logic
httpClient: ajax
};
const arrLogic = [npmSearchLogic];
const logicMiddleware = createLogicMiddleware(arrLogic, deps);
const store = createStore(reducer, initialState,
applyMiddleware(logicMiddleware));
const ConnectedApp = connect(
state => ({
results: state.list,
fetchStatus: state.fetchStatus
}),
{
npmSearch
}
)(App);
function App({ results, fetchStatus, npmSearch }) {
return (
<div>
<div>Search npmsearch.com for packages</div>
<div>Status: {...