使用Redux,我希望我的reducer的初始状态之一基于AJAX调用。基本上,我使用AJAX来获取一些数据,然后将其置于Reducer的状态。但是,由于进行了AJAX调用,在完成AJAX调用之前,将reducer初始化为初始状态。您将如何让reducer等待AJAX调用完成之后再设置初始状态?
export default () => {
const store = createStore(
combineReducers({
settings: settingsReducer,
})
);
return store;
}
var settingsDefaultState;
$.ajax({
url: 'view/content/settings.xml',
type: 'GET',
success: function(xml) {
//DOING SOME STUFF WITH THE XML FILE HERE
settingsDefaultState = {
test: "HELLO WORLD"
}
},
complete: function() {
console.log("Settings Loaded...");
}
});
export default (state, action) => {
var tempState = {...state};
switch (action.type) {
default:
return tempState;
}
}
答案 0 :(得分:1)
我建议您将状态初始化为from pdfminer.pdfinterp import PDFResourceManager, PDFPageInterpreter
from pdfminer.converter import TextConverter
from pdfminer.layout import LAParams
from pdfminer.pdfpage import PDFPage
from io import StringIO
from nltk.tokenize import sent_tokenize
def convert_pdf_to_txt(path):
rsrcmgr = PDFResourceManager()
retstr = StringIO()
codec = 'utf-8'
laparams = LAParams()
device = TextConverter(rsrcmgr, retstr, codec=codec, laparams=laparams)
fp = open(path, 'rb')
interpreter = PDFPageInterpreter(rsrcmgr, device)
password = ""
maxpages = 0
caching = True
pagenos=set()
for page in PDFPage.get_pages(fp, pagenos, maxpages=maxpages, password=password,caching=caching, check_extractable=True):
interpreter.process_page(page)
text = retstr.getvalue()
sentences = sent_tokenize(text)
for s in sentences:
print(s)
print("\n\n")
状态。这样,您可以将发生的事情反馈给用户。然后,当ajax调用返回响应时,您将触发将状态设置为所需的下一个状态的操作。