1

ビューを生成するために Facebook ComponentsKit を使用しています。

私は現在、アプリの状態を変更し、ビューの更新をトリガーするための「フラックス」アーキテクチャに移行しています。

私が抱えている主な問題は、すべての状態の変化が UI の更新をトリガーするわけではないということです。それを回避するための「一般的な」メカニズムがわかりません。

基本的に、アプリの状態は「ビュー モデル」を表す「大きな」「JSON」です (ネイティブ型のオブジェクトに解析されます)。JSON には、すべてのビュー宣言とその初期値が含まれています。(JSON は非常に複雑です)

たとえば、「ページャー」コンポーネントとナビゲーションの「次へ」ボタンを含むビュー階層を表す簡略化された JSON :

{
   ... views ...
        {
           "pager" : {
                "id" : "pager-id-xxx",
                "currentPage" : 0,
                "pages" : [
                      ....pages....
                      {},
                      {},
                      ....pages....
                ]
            },
            ...
            "navigation-next-button" : {
                 "id" : "navigation-next-button-id-xxxx",
                 "target" : "pager-id-xxx"
            }
        },
   ... views ...
}

私の「フラックス」の抽象化は次のようになります。

// "Action" portion
@interface ChangePageAction

@property id destinationId; // very simplified action. wraps the destination "id"

@end

@implementation ChangePageReducer

-(JSON)reduce:(JSON)initialJSON  action:(ChangePageAction *)changePageAction {
      // the "reduce" portion finds the node of the pager (in the JSON) and changes the value by +1
      // something like:
      // find the node in the JSON with the changePageAction.destinationID
    Node *nodeCopy = getNodeCopy(initialJSON,changePageAction.destinationId);
    replaceValue(nodeCopy,nodeCopy.currentPage + 1);
    // according to FLUX doctrine we are supposed to return a new object
    return jsonCopyByReplacingNode(nodeCopy); // a new JSON with the updated values 
}

// the navigation button triggers the new state
@implementation NavigationNextButton {
   id _destination; // the target of this action
   FluxStore _store; // the application flux store
}

-(void)buttonPressed {
   ChangePageAction *changePage = ...
   [_store dispatch:changePage];

}
@end

私の「ビューコントローラー」では、「状態の更新」コールバックを取得します

@implementation ViewController 

-(void)newState:(JSON)newJSON {
    // here all of the view is re-rendered
    [self render:JSON];


   //The issue is that I don't need or want to re-render for every action that is performed on the state. 
   // many states don't evaluate to a UI update
   // how should I manage that?
}
@end
4

1 に答える 1

2

残念ながら、ComponentKit でこれを行う簡単な方法はありません。React にはshouldComponentUpdateがありますが、ComponentKit には同等のものはありません。

幸いなことに、ComponentKit はすべてのコンポーネントを再構築するのに十分なほどスマートである必要があり、実際には何も変更されていないことに気付き、最終的に UIKit の変更は行われません。

悪いニュースは、それを行うためにかなりの量の CPU を消費することです。

于 2016-03-30T21:47:31.593 に答える