【发布时间】:2017-09-11 16:49:04
【问题描述】:
我正在尝试使用草稿 js 构建内容编辑器。确切地说,该功能是从像 Facebook 这样的 url 中提取数据。但我被这部分困住了。回调不起作用。
首先我用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 数据,我必须通过 props 将数据传递给我的组件并渲染它。
答案更新
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 - 是你的 passit 变量填充了
passit = response.data。你能检查一下console.log吗? -
我的主要目标是将 url 数据传递给我的组件,是的 passit 有一些有效的 url 数据我已经测试过了..@nash_ag
-
@MaximShoustin 使用草稿 js 嵌入 url 的任何建议或任何替代方法
标签: javascript ajax reactjs axios draftjs