誰か助けてもらえますか?私は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