編集:はアクション機能を単一の引数をバインドするために簡単な例を追加しました。
私はこの問題の解決策がたくさんあると思いますが、私が通常行っていることを記述しようとします(コードはテストされていません)。あなたが以下のリスト項目コンポーネントcomponents/list_item.js
を持っているとしましょう:
import React from 'react';
class ListItem extends React.Component {
render() {
const { clickHandler, label } = this.props;
return <li onClick={clickHandler}>{label}</li>;
}
}
// add default props and propTypes here...
export ListItem;
とリストラッパーコンポーネントcomponents/list.js
:
import React from 'react';
import ListItem from './list_item.js';
class List extends React.Component {
renderItem({ clickHandler, label, arg }) {
return <ListItem clickHandler={arg ? this.props[clickHandler].bind(this, arg) : this.props[clickHandler]} label={label} />;
}
renderList() {
const { items } = this.props;
return items.map((item) => {
return renderItem(item);
});
}
render() {
return <ul>{this.renderList()}</ul>;
}
}
// add default props and propTypes here...
export List;
私は新しいリストを作成したい場合はcontainers/my_list.js
を次のように私はちょうど新しいコンテナを作成します:
import List from '../components/list';
import {useDeps, composeWithTracker, composeAll} from 'mantra-core';
export const composer = ({}, onData) => {
const items = [
{
clickHandler: 'goto',
label: 'Goto document',
arg: 'MyRoute'
},
{
clickHandler: 'remove',
label: 'Remove document'
}
];
onData(null, {items});
};
export const depsMapper = (context, actions) => ({
goto: actions.myList.goto,
remove: actions.myList.remove,
context:() => context
});
export default composeAll(
composeWithTracker(composer),
useDeps(depsMapper)
)(List);
このコンテナは、例えばにリンクされているこれらのアクションactions/my_list.js
:
export default {
goto({ FlowRouter }, route) {
FlowRouter.go(route);
},
remove({ Collections }, _id) {
Collections.Documents.remove({ _id })
}
}
このパターンを使用すると、リストの一般的な構造が作成されます。このパターンが有用で、私の現在の解決策が改善されているかどうかを教えてください。コンテナを設計する際には、クリックハンドラに引数をバインドすることもできます(上記の例では図示していません)。
答えは次のとおりです。1)ルーティングの試行ごとにアクションを作成する必要がありますか?そして、2)実際には、任意のURLに対して一般的なルーティングアクションを実行するのではなく、行きたいすべてのURLに対して特定のアクションを作成する必要があります。もしそうなら:q1) ' Domi
すべてのURLに対して新しいアクションを作成する必要はありません。必要なアクションにリンクできるdepsMapperを使用してください。私は通常、 'shared.js'(または同様のもの)というファイルに頻繁に使用されるアクションを入れ、モジュール全体で使用します。ですから、 'goto:actions.myList.goto'は' goto:actions.shared.goto'です。今私の例では、引数をアクション関数にバインドする方法が示されていないことがわかりました。私は簡単な例で更新します。 –