私のreact-router
アプリの各ページにあるこの機能を持っています。私の機能をどのようにリファクタリングするのですか?
submitForm(e) {
const redirect = this.props.location.query.redirect;
const age = this.refs.age.value;
const gender = this.refs.gender.value;
const ethnicity = this.refs.ethnicity.value;
if (!age || !gender || !ethnicity) {
this.props.dispatch(
showError({
type: 'SHOW_MODAL',
modalType: 'SHOW_ERROR',
modalProps: {
onClose: hideModal,
text: 'Please complete all fields',
},
})
);
} else {
this.props.dispatch(addDetails(age, gender, ethnicity));
if (redirect) {
this.props.router.push('6');
} else {
this.props.router.push('4');
}
}
}
私は反応するのが新しいです、これをどのようにして各コンポーネントに小道具として渡すことができますか?各コンポーネントでコードを繰り返すのではなく、
このようなファイルをhelpers
に入れることはできますか?
import helpers from './helpers';
import './index.css';
const store = configureStore();
console.log({ store });
render(
<Provider store={store}>
<Router history={browserHistory} routes={routes} {...helpers} />
</Provider>,
document.getElementById('root')
既にヘルパーがいれば、それを 'export submitForm'の中に入れてください。それから 'import {submitForm} 'を'。/ helpers'' –