0

i do have a component in which

state={
mediaJson=[]
} 

and i have a reducer which calls an API and returns data and i used

static getDerivedStateFromProps(props, state) {
return{
mediaJson:props.data
} 

i am getting the data and ist getting displayed

as per my understanding its kind of setting to the state named mediaJson ,again in another click function

tabClicks = (val) => {
const mediaContent = [...this.state.mediaJson];
}

and tried to console mediaContent this am getting undefined

midhun k
  • 546
  • 1
  • 12
  • 28
  • 1
    First off, reducer is [supposed](https://redux.js.org/basics/reducers/#handling-actions) to stay pure function (which means, it shouldn't do any side-effects, like fetching API data); Next, you're [misusing](https://uk.reactjs.org/blog/2018/06/07/you-probably-dont-need-derived-state.html#when-to-use-derived-state) `getDerivedStateFromProps`. Your proper choice would be to use `connect()` and [`mapStateToProps`](https://react-redux.js.org/using-react-redux/connect-mapstate#defining-mapstatetoprops) to bind your component to certain part of global state or use hook alternatives. – Yevhen Horbunkov Feb 07 '20 at 16:45
  • If you wish, you may post wider code context and broader explanation of what you're trying to achieve and I may suggest conventional approach to solve your problem with React/Redux. – Yevhen Horbunkov Feb 07 '20 at 16:45
  • @YevgenGorbunkov didnt get exactly ,cant it be used for API calls ? it shouldnt do any side effects means ? – midhun k Feb 07 '20 at 17:02
  • If you need to do API calls, you may employ action creators for that purpose, possibly, together with some middleware (like redux-thunk). As being said, state your problem clearly, append your code to see approach you're following them you may get relevant answer. – Yevhen Horbunkov Feb 07 '20 at 21:40

0 Answers0