Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

112
Views
Getting the state with rematch

I am learning rematch/redux. I can't get the state to show with the API. I have the model imported in index.js along with the store and the provider. These are my reducers/effects:

import { getItems } from './service'

const products = {
  state: {
    products: [],
  },

  reducers: {
    setProducts(state, products) {
      return {
        ...state,
        products,
      };
    },
  },
  effects: {
    async loadProducts() {
      const products = await getItems() // <-- This is the api working normally
      this.setProducts(products)
    },
  }

}

export default products

And this is my component:

import './App.css';
import { connect } from 'react-redux';
import React, { useEffect } from 'react';

const mapStateToProps = ({ products }) => {
  return {
    ...products
  }
}

const mapDispatchToProps = ({ products }) => {
  return {
    ...products
  }
}

const App = ({ products }) => {

  useEffect(() => {
    console.log(products)
  })

  return (
    <div className="App">
      {console.log(products)}
    </div>
  )
}

export default connect(mapStateToProps, mapDispatchToProps)(App);

I am not sure what I am missing.

Thank you.

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

I'm Rematch maintainer, you should review our documentation or consider buying the official Redux made easy with Rematch book where you'll learn all this questions.

I highly recommend using React-Redux hooks instead of connect method.

import './App.css';
import { useSelector, useDispatch } from 'react-redux';
import React, { useEffect } from 'react';

const App = () => {
  const dispatch = useDispatch();
  const { products } = useSelector(rootState => rootState.products)
  useEffect(() => {
    dispatch.products.loadProducts();
  }, []);

  return (
    <div className="App">
      {console.log(products)}
    </div>
  )
}

export default App;

Be careful with hooks, you're forgetting to add the deps array to useEffect, any code you add there will run infinitely

Your Rematch models looks fine, you just need to work more on React essentials :)

about 4 years ago · Juan Pablo Isaza Report

0

Hope this will helpfull

import React, { useEffect} from 'react';
import { connect } from 'react-redux';

export function UserIndex({users,userroles,getUsers,getUserRoles  }){
 // Load data on component mount
  useEffect(() => {
    getUsers({});
    getUserRoles();
  }, [getUserRoles, getUsers]);
  console.log(users);
  console.log(userroles);

return (
    <></>
   );

  }
 const mapState = (state) => ({
     users: state.user.users,
     userroles :state.listHelper.userroles,
  });

 const mapDispatch = (state) => ({
    getUsers: (payload) => state.user.getUsers(payload),
    getUserRoles : () => state.listHelper.getUserRoles(),
 });
  
 const UserIndexContainer = connect(mapState, mapDispatch)(UserIndex);
 export default UserIndexContainer;
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!