0

EDIT: I think the issue is with my reducers I only have one reducer called "filmsReducer" where I do this at the end :

export default combineReducers({
  films: filmsReducer
});

I'm doing an app in React Native using Redux,

I want to get the initialState values below in a component :

const initialState = {
  name: "",
  likedFilms: [299534, 49530, 629],
  dislikedFilms: [100241, 559969]
};

const filmsReducer = (state = initialState, action) => {
  const { likedFilms, dislikedFilms } = state;
  switch (action.type) {
    case ADD_FILM:
      if (action.array === "like") {
        const newLikedFilms = [...state.likedFilms, action.payload];

        return {
          ...state,
          likedFilms: newLikedFilms
        };
      } else {
        const newDislikedFilms = [...state.dislikedFilms, action.payload];

        return {
          ...state,
          dislikedFilms: newDislikedFilms
        };
      }

    default:
      return state;
  }
};

And here's the component, I want to get likedFilms array from the redux state in the props of this component, but the console log doesn't work :

class LikedScreen extends Component {
  constructor(props) {
    super(props);
  }

  componentDidMount() {
    console.log(this.props.likedFilms); <-- doesn't work
  }
}

const mapStateToProps = state => ({
  likedFilms: state.likedFilms
});

export default connect(mapStateToProps)(LikedScreen);
8
  • What's the name of your reducer? Commented Nov 21, 2019 at 12:19
  • the name is FilmsReducer Commented Nov 21, 2019 at 12:22
  • Have you added filmsReducer to the rootReducer? Commented Nov 21, 2019 at 12:23
  • Try logging your state - state.likedFilms is undefined, because likedFilms is not your reducer, hence not your state. Commented Nov 21, 2019 at 12:23
  • @Dan I don't have a rootReducer, only a filmsReducer and at the bottom of the filmsReducer, I use combineReducers({ films: filmsReducer }) Commented Nov 21, 2019 at 12:27

2 Answers 2

4

Regarding your comment, you probably have to adapt your code to the following:

Edit Regarding another comment of yours, you need to change it to films instead of FilmsReducer:

const mapStateToProps = state => ({
  likedFilms: state.films.likedFilms
});
Sign up to request clarification or add additional context in comments.

1 Comment

thank you it works, because of the way I named my reducer in the export of the FilmsReducer.js
0

It will be like, use reducer name as while mapping props in Component

class LikedScreen extends Component {
  constructor(props) {
    super(props);
  }

  componentDidMount() {
    console.log(this.props.likedFilms); <-- doesn't work
  }
}

const mapStateToProps = state => ({
  likedFilms: state.films.likedFilms
});

export default connect(mapStateToProps)(LikedScreen);

5 Comments

it says TypeError: TypeError: undefined is not an object (evaluating 'state.filmsReducer.likedFilms'), I only have one reducer : the filmsReducer
it might be because you didn't register this reducer in root reducer
ok so I will always need a rootReducer ? I can't juste use my filmsReducer ?
that was because you registered your filmsReducer with the name films in combineReducers({ films: filmsReducer })
okkkkk so in the mapStateToProps, I have to put likedFilms: state.films.likedFilms

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.