私は様々なプロパティの合計を計算するテーブルを作成することになっている2つの反応クラスを持っています。リアクションレンダリングが順番に実行されない
const TableComponent = React.createClass({
resetRowTotals: function() {
console.log("Resetting");
this.total1 = 0;
this.total2 = 0;
this.total3 = 0;
this.total4 = new Set();
},
addToRowTotals: function(total1, total2, total3, total4) {
console.log("Updating");
this.total1 += ...;
this.total2 += ...;
this.total3 += ...;
this.total4.add(...);
},
render: function() {
this.resetRowTotals();
this.tableRows = this.props.items.map(item => React.createElement(TableRow, {..., addToRowTotals: this.addToRowTotals}));
console.log(this.total1, this.total2, this.total3, this.total4);
return React.createElement("table", {id: "..."},
React.createElement("thead", {},
React.createElement("tr", {},
React.createElement("th", {}, ...),
React.createElement("th", {}, ...),
React.createElement("th", {}, ...),
React.createElement("th", {}, ...),
React.createElement("th", {}, ...)
)
),
React.createElement("tbody", {},
React.createElement("tr", {},
React.createElement("td", {}, ...),
React.createElement("td", {}, ...),
React.createElement("td", {}, ...),
React.createElement("td", {}, ...),
React.createElement("td", {}, ...)
),
this.tableRows
)
)
}
});
をそして、私の行の構成要素のために私が持っている:私のメインクラスで
は、私が持っている
const TableRow = React.createClass({
getVal2: function() {...},
getVal3: function() {...},
getVal4: function() {...},
render: function() {
let val1 = this.props....;
let val2 = this.getVal2();
let val3 = this.getVal3();
let val4 = this.getVal4();
this.props.addToRowTotals(val1, val2, val3, val4);
return React.createElement("tr", {},
React.createElement("td", {}, this.props....),
React.createElement("td", {}, val1),
React.createElement("td", {}, val2),
React.createElement("td", {}, val3),
React.createElement("td", {}, val4)
)
}
});
しかし、私が取得する表を見てみるとき、すべてのテーブルの行は問題ありませんが、テーブルの最上部にある行全体がすべて0を表示しています
私の機能の実行順序を見るためにコンソールを見ると、表示されます:
Resetting
0 0 0 Set(0) {}
Updating
私は何を期待:
Resetting
Updating
(total1's value) (total2's value) (total3's value) Set(x) {(total4 values)}
コードが順番に実行なっていることは表示されません。
合計がリセットされ、次にaddToRowTotals関数を呼び出すメイン行が作成され、次にデータが合計に対して正しいことを確認するために、合計4つの合計値が記録されます。
テーブルローを作成する前にコンソールが合計値を記録しているのはなぜですか?
これは実際には間違っていると思いますが、まずは 'addToRowTotals'をメイン要素にバインドしてみましょうか?基本的に、(Map呼び出しの) 'TableRow'コンポーネントに渡す小道具は、次のようになります:' {...、addToRowTotals:this.addToRowTotals.bind(this)} ' – iceman
また、コンポーネントの状態を使用するほうが意味をなさないでしょう。 – iceman
addToRowTotalsのバインディングは、どのような方法でも機能に影響を与えていないようです。 私はval#変数がどこに格納されているのか疑問に思っています。コンポーネントを互いに独立させたい(しかし、子供がまだ新しいデータで特定の親関数を呼び出せるようにすることが必要なため)。 私はこれを行う前に、値を計算してレンダリングが発生する前に親に戻すためにcomponentWillUpdate(nextProps、nextState)を使用してみましたが、コードがさらに信頼できなくなり、使用できなくなりました。興味深い; – S0rn