即時通訊使用React,除了'事件'之外,我想傳遞一些參數,所以我決定使用高階函數。通過高階函數傳遞參數
但是,它不識別傳遞給高階函數的'id'。
容器組件
...
const mapDispatchToProps = (dispatch) => {
return({
dispatchSelectElement : function(e){
console.log(id); // Error: id is not defined.
dispatch(selectElement(id, type, pos));
},
...
});
};
const C_ElementParent = connect(mapStateToProps, mapDispatchToProps)(ElementParent);
有,所述容器部件&下面呈現組件之間位於另一組件。正如通過console.log所報告的那樣,道具正常傳遞。上面的dispatchSelectElement
通過下面的eventProps
。
表象部件
const Element = ({ id, colorName, eleProps, eventProps }) => {
let handleDispatchSelectEle = function(id){
return eventProps.dispatchSelectElement;
}
return(
<g id = { id }>
<path onMouseDown = { eleProps.get("mouseDown") && handleDispatchSelectEle(id)} />
</g>
);
};
你在哪裏傳遞一個ID,具體功能?在你向我們展示的代碼中,發生錯誤的地方確實沒有'id'變量。你爲什麼認爲它應該在那裏定義? – Bergi
@Bergi''''在'path'元素的handleDispatchSelectEle函數中傳遞。然後handleDispatchSelectEle返回dispatchSelectElement函數。 – Kayote
呃,我現在看到了。這不是它的工作原理。我會寫一個答案。 – Bergi