i have some problem i can't figure out. i have some component inside i'm using react-table plugin.
on componentDidMount
method i have to call redux action which get data from api and then i'm passing this data to react-table.
here is my componentDidMount method where i call to redux action.
componentDidMount() {
this.props.getData()
}
here is my action:
export const FETCH_DATA_START = 'FETCH_DATA_START'
export const FETCH_DATA_SUCCESS = 'FETCH_DATA_SUCCESS'
export const FETCH_DATA_FAILED = 'FETCH_DATA_FAILED'
export const getOffBoardingData = () => {
return (dispatch) => {
dispatch({
type: FETCH_DATA_START
})
fetch(baseUrl, {
credentials: "include",
method: "GET",
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
}
})
.then(res => res.json())
.then((res) => {
dispatch({
type: FETCH_DATA_SUCCESS,
payload: res.result
})
})
.catch((err) => {
dispatch({
type: FETCH_DATA_FAILED,
payload: 'error'
})
})
}
}
and here is my reducer.
import { FETCH_DATA_START, FETCH_DATA_SUCCESS, FETCH_DATA_FAILED } from 'store/actions/getData'
let initialState = [{
items: null
}]
export default (state = initialState, action) => {
switch (action.type) {
case FETCH_DATA_START:
return {...state}
case FETCH_DATA_SUCCESS:
return { ...state, items: action.payload}
case FETCH_DATA_FAILED:
return {...state}
default:
return state
}
}
everything work well here i think.
here is in my component i'm trying to make conditional rendering like:
if (this.props.getDataReducer.items != null) {
return (<div>
<p>Loading...</p>
</div>);
} else {
return(
<ReactTable className="-striped -highlight"
data={this.props.getDataReducer.items}
columns={columns}
/>
)
}
here is i make conditional rendering for my data. If items is null return some loading text else return react-table with data which came from api.
but i don't know how react-table trying to rendering when data null too.
where i return react-table i'm trying to make console.log my data but it's return undefined
sometimes component rendering without problem sometimes just giving error that data is undefined.
I make mistake about conditional rendering?
i tried to make that: component will wait to data and only when data will come that time component will be rendering.
where i make mistake?
if (this.props.getDataReducer.items) { //react table } else { // loading }
– lochawala