我正在尝试构建一个带草稿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'中检查。 –