1
我有一個名爲PlatformMain
的組件,它當前依賴於來自組件文件內部定義的Phoenix的全局對象channel
。Phoenix + React Native和Redux:我應該在哪裏放置頻道對象?
let channel;
let socket = new Socket("...", {params: {token: window.userToken}});
socket.connect();
class PlatformMain extends React.Component {
componentWillMount() {
this.connectUser();
}
connectUser() {
const { user } = this.props;
channel = socket.channel("user_pool:" + user.email, { app: APP });
this.setupChannel();
}
setupChannel() {
channel.join()
.receive("ok",() => { console.log("Successfully joined call channel") })
.receive("error",() => { console.log("Unable to join") })
channel.on("match_found", payload => {
...
});
...
}
如果用戶按下按鈕,我希望分派一個動作以及將消息推送到該通道。
onPress() {
console.log("APPROVE_MATCH");
const { peer, waitForResponse } = this.props;
approveMatch(peer);
channel.push("approve_match", { // <------ want to put this somewhere else
"matched_client_email": peer.email,
});
}
我的問題是,如果我想「reduxify」channel.push調用,我應該把它放在哪裏?感覺很奇怪,因爲它是一個API調用,所以在其他地方沒有channel.push(...)
。我打算把它寫成一個傳奇使用REDX傳奇像這樣:
function* approveMatch(action) {
const peer = action.payload.peer;
channel.push("approve_match", { // <------- but how would I get the same channel object?
"matched_client_email": peer.email,
});
}
export default function* watchMatchingStatus() {
yield takeEvery(matchingStatusActions.APPROVE_MATCH, approveMatch);
}
但我不需要指向相同的通道對象?我會怎麼做?如果我將channel
的初始化放在它自己的文件中,並且我將它導出並導入到多個位置,它是否不會多次執行文件(並因此多次加入該通道)?
非常感謝! – Edmund