有人可以幫助我解決這個問題。我可以看到api的響應,但當頁面呈現時,響應太遲而無法呈現。我找不到我做錯了什麼。如果有人能解釋我會很感激。由於React Redux - 渲染前加載響應數據
下面是我的減速器
export default function reducer(state = {
responseCode : {
},
fetching: false,
fetched: false,
error: null
}, action){
switch(action.type){
case 'FETCH_RESPONSECODE_PENDING' : {
return { ...state, fetching: false}
break;
}
case 'FETCH_RESPONSECODE_ERROR' : {
return { ...state, fetching: false, error: action.payload }
}
case 'FETCH_RESPONSECODE_FULFILLED' : {
return{
...state,
fetching: false,
fetched: true,
responseCode: action.payload
}
break;
}
}
return state;
}
// SearchResponseCode組件
handleSearch(event){
event.preventDefault();
}
render(){
return (
<form>
<div className="col-xs-8">
<input type="number" className="form-control" placeholder="e.g. main mailing response code or recruitment campaign code" ref="responseCode" />
</div>
<div className="col-xs-4">
<button className="btn btn-default" onClick={this.handleSearch.bind(this)}>Search</button>
</div>
</form>
)
}
//主要成分
import SearchResponseCode from './search-response-code'
import { connect } from 'react-redux'
import { fetchResponseCode } from '../../actions/responseCodeActions'
@connect((store)=>{
return{
responseCode: store.responseCode.responseCode
};
})
fetchResponseCode(){
this.props.dispatch(fetchResponseCode(brandUrl, 2570010))
}
render(){
const { responseCode } = this.props
console.log(this.responseCode)
return(
<Tabs selectedIndex={0}>
<TabList>
<Tab>Search By Responce Code</Tab>
<Tab>Search By Item Code</Tab>
<Tab>Searh All</Tab>
</TabList>
<TabPanel>
<SearchResponseCode fetchResponseCode={this.fetchResponseCode.bind(this)} />
</TabPanel>
<TabPanel>
<SearchItemCode />
</TabPanel>
<TabPanel>
</TabPanel>
</Tabs>
)
}
}
//行動
import axios from 'axios'
export function fetchResponseCode(brandUrl, responseCode){
let url = brandUrl + '/api/offer/' + responseCode;
return function(dispatch){
axios.get(url)
.then((response) => {
dispatch({
type : 'FETCH_RESPONSECODE_FULFILLED',
payload : response.data
})
})
.catch((err) => {
dispatch({
type : 'FETCH_RESPONSECODE_ERROR',
payload : err
})
})
}
}
你的SearchResponseCode組件並不都在那裏,這可能會幫助你。 Axios使用承諾發送給商店,當響應回來時應該觸發渲染。換句話說,對於渲染來說,響應不會太遲,您可能不會使用狀態的正確部分。 –
嗨@JoPeyper謝謝你的回覆。你能找出我做錯了什麼嗎? – Mesmerize86
@ Mesmerize86單擊SearchResponseCode組件中的Search btn時發生了什麼?頁面重新呈現之前等待一會兒? – semuzaboi