0

I have a Component who calls a function via props

function mapState(state) {
  return state;
}

const actionCreators = {
  getById: productActions.getById,
};

export default connect(mapState, actionCreators)(ShopBook);

this function is used at the constructor of the Component to retrieve content from my API and store the response into a reducer

  constructor(props) {
    super(props);

    this.props.getById(this.props.match.params.slug)

    this.state = {
    }
  }

then in the render I use the store

  render() {

    const product = store.getState().product.item

    return (
       // my code
    );
 }

my problem is, if I change content in the database the function does not retrieve the latest change vía the api, I only see the changes if I force the browser by pressing the Shift key while click reload.

how can I force to get the latest content, where do I need to use the function instead the constructor?

I already tried this solution: How to fetch data when a React component prop changes?

Roberto
  • 93
  • 1
  • 1
  • 9
  • Are you saying that even if you refresh the page (a normal "refresh" button click) the page renders with outdated information? – jered Mar 23 '20 at 18:59
  • yes, if I only do a normal "refresh", or browse the web and come back to the page it renders outdated information – Roberto Mar 23 '20 at 19:08

1 Answers1

0

You are using redux wrongly. You should pass correct mapStateToPop. If u want to render. Since, u mappping entire state. Redux find no change. That is why it is not rerendering.

Sample:

const mapStateToProps = (state, ownProps) => ({
  product: state.product
})

export default connect(mapState, actionCreators)(ShopBook);
xdeepakv
  • 7,835
  • 2
  • 22
  • 32
  • I changed as the sample code (for sure I was using wrong the redux), and in the render also changed for this code `const product = this.props.product.item`, it reloads, but the content is not updated, in the response (DevTools > Network) after calling `this.props.getById(this.props.match.params.slug)` I still can view the previous info – Roberto Mar 23 '20 at 19:36