Skip to content

Instantly share code, notes, and snippets.

@derekahn
Last active January 24, 2021 23:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save derekahn/a2b2c0f4616c687b7fdeac1c8e4cfcf9 to your computer and use it in GitHub Desktop.
Save derekahn/a2b2c0f4616c687b7fdeac1c8e4cfcf9 to your computer and use it in GitHub Desktop.
Iterating in react-router
/* containers/Main/formProps.js */
export default [{
path: 'quiz',
name: 'favorite-car',
label: 'What is your favorite car?',
type: 'text',
placeholder: 'bat mobile'
}, {
path: 'quiz1',
name: 'favorite-cat',
label: 'What is your favorite cat?',
type: 'text',
placeholder: 'bengal'
}, {
path: 'quiz2',
name: 'user-age',
label: 'How old are you?',
type: 'number'
}];
/* containers/Main/FormTemplate.js */
export default (questions) =>
<form>
{questions.map((question, i) =>
<div className={question.name} key={`question-${i}`}>
<label for="">{question.label}</label>
<input type={question.type} placeholder={question.placeholder} />
</div>
)}
<button type="submit>Submit</button>
</form>;
/* routes.js */
import { Route, IndexRoute } from 'react-router';
import Index from 'containers/Index';
import Main,{ FormTemplate, formsProps } from 'containers/Main;
export default (
<Route path="/">
<IndexRoute component={index}>
<Route component={Main}>
{formsProps.map((props, i) =>
<Route
key={`route-form-${i}`}
path={props.path}
component={simulateExport(FormTemplate, props)} />
)}
</Route>
</Route>
);
// This simulates a wrapped es6 exported component with props
// => () => <Component {...props} />;
function simulateExport(component, props) {
return () => React.createElement(component, props);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment