所以我有一個輸入:努力沒有得到響應的一個變化下旬
的onChange此輸入的值獲取的更新,並存儲在道具持久性,但是這是無關的問題。
如果在數據庫中找到輸入值,我將在字段旁邊添加複選標記。
我已經有調用API,並回答設立(我的終極版減速):
import EventTypes from '../EventTypes';
const initialState = {
response: {},
};
export default (state = initialState, action) => {
switch (action.type) {
case EventTypes.EXISTS_FULFILLED.type:
console.log(action.payload.body().data());
return { ...state, response: action.payload.body().data() };
default:
return state;
}
};
注意上面的控制檯日誌正確打印真/假值(+送價值,我得到兩個從服務器返回)。
但在調用此組件:
const defaultProps = {
onChange:() => {
},
value: { type: '' },
provider: { type: '' },
};
class InputsComponent extends Component {
onChange =() => {
this.props.onChange(this.props.id);
};
updateFirstInput(event) {
const { onChange, exists, response } = this.props;
const payload = {
objectType: this.props.placeholder.toLowerCase(),
objectName: event.target.value,
};
exists(payload);
this.setState({ firstInput: event.target.value });
onChange({ value: {
...this.state,
firstInput: event.target.value,
} }, this.props.id);
setTimeout(() => {
this.setState({ exitsStore: response });
}, 200);
setTimeout(() => {
console.log(this.state.exitsStore);
}, 1000);
}
控制檯日誌都是空的第一次「updateFirstInput()」之稱,然後始終一個人「的onChange」的背後被準確。
這是「你好」:
[input value] [ console log of value for which presence on the server is true/false]
"h" ""
"he" "h"
"hel" "he"
"hell" "hel"
"hello" "hell"
一段時間後,我想通了,這是怎麼回事。
我已經試過這樣:
const defaultProps = {
onChange:() => {
},
value: { type: '' },
provider: { type: '' },
};
class InputsComponent extends Component {
onChange =() => {
this.props.onChange(this.props.id);
};
checkExists(object){
const { exists, response } = this.props;
const payload = {
objectType: this.props.placeholder.toLowerCase(),
objectName: object,
};
exists(payload);
return response;
}
updateFirstInput(event) {
const { onChange } = this.props;
this.setState({ firstInput: event.target.value });
onChange({ value: {
...this.state,
firstInput: event.target.value,
} }, this.props.id);
const response = await this.checkExists(event);
console.log(response);
this.setState({ exitsStore: response });
console.log('stored data :');
console.log(this.state.exitsStore);
}
但出於某種原因,人們在網上建議我們使用的await沒有提供上下文。以上內容甚至不可編輯。 (npm開始將失敗)
甚至在該ESLint表明「await是期待換行符或分號的保留字」之前。
但我很確定上面甚至正確syntaxe也不會工作。
它可能不會等待,但我如何設法從服務器(理想情況下)在一個單一的功能,即調用輸入更新相同的功能來填充響應。
Constrainst:
據我知道你不能兼得的onblur和的onChange相同的輸入這樣以來我已經使用的onChange沒有的onblur。
我不能在父母或其他人中調用任何這樣的東西:有多個輸入字段作爲子元素,我需要將值檢查和複選標記出現在孩子內部,以便能夠將值匹配到一起。
UPDATE:
,如果我只是刪除定時器:
const defaultProps = {
onChange:() => {
},
value: { type: '' },
provider: { type: '' },
};
class InputsComponent extends Component {
onChange =() => {
this.props.onChange(this.props.id);
};
updateFirstInput(event) {
const { onChange, exists, response } = this.props;
const payload = {
objectType: this.props.placeholder.toLowerCase(),
objectName: event.target.value,
};
exists(payload);
this.setState({ firstInput: event.target.value });
onChange({ value: {
...this.state,
firstInput: event.target.value,
} }, this.props.id);
this.setState({ exitsStore: response });
console.log(this.state.exitsStore);
}
...兩個電話後面。
使用'await',函數需要'async'。你可以同時使用'onChange'和'onBlur'事件。你可以添加'this.props.onChange'的代碼嗎? – bennygenel
完成。它是同一班的一部分。並感謝信息不知道onChange和onBlur可以一起使用。 – tatsu
還有什麼你的意思是功能需要異步?我的功能和JS中的所有功能一樣明顯是異步的。事情只是在一個完全隨機的順序執行(這是令人討厭的國際海事組織)我沒有找到任何解決方案來打擊這個其他然後添加計時器 – tatsu