我正在嘗試構建一個帶草稿js的contentEditor。這個功能的確是從像Facebook這樣的URL中提取數據。但我堅持這部分。回叫不起作用。回調不在Ajax請求中工作?
首先我包裹着我的狀態compositeDecorator
篩選
constructor(props) {
super(props);
const compositeDecorator = new CompositeDecorator([
.... {
strategy: linkStrategy,
component: decorateComponentWithProps(linkComp, {
passit
})
}
....
]);
}
// This is my strategy
function linkStrategy(contentBlock, callback, contentState) {
findLinkInText(LINK_REGEX, contentBlock, callback)
}
function findLinkInText(regex, contentBlock, callback) {
const text = contentBlock.getText();
let matchArr, start;
if ((matchArr = regex.exec(text)) !== null) {
start = matchArr.index;
let URL = matchArr[0];
console.log(URL);
axios.post('/url', {
url: URL
}).then(response => {
passit = response.data
//not working
callback(start, start + URL.length)
})
//working
callback(start, start + URL.length)
}
}
如果回調將無法正常工作,該組件將不會呈現.. 我不知道這是一個基本的JavaScript問題。但事情是我想從我的服務器獲取URL數據,我必須通過道具傳遞數據到我的組件並呈現它。
更新答案
function findLinkInText(regex, contentBlock, callback) {
const text = contentBlock.getText();
let matchArr, start;
if ((matchArr = regex.exec(text)) !== null) {
start = matchArr.index;
let url = matchArr[0];
axios.post('/url', {
url: URL
}).then(response => {
passit = response.data
handleWithAxiosCallBack(start, start + matchArr[0].length, callback)
}).catch(err => console.log(err))
}
}
function handleWithAxiosCallBack(start, startLength, callback) {
console.log(callback); //Spits out the function But Not working
callback(start, startLength)
}
從你的例子我看不出你是如何將回調傳遞給'linkStrategy'的。請發佈更多關於'strategy:linkStrategy'的詳細信息 –
先生,從LinkStrategy我將回調傳遞給其他函數,我從那裏找到url匹配回調將被執行@MaximShoustin – Nane
@Nane - 你的passit變量填充了'passit = response。 data'。你可以在'console.log'中檢查。 –