My attempt to use the Redux method for displaying data in my testing environment has not yielded

  • I've been attempting to showcase the data in a Redux-friendly manner.
  • When the advanced sports search button is clicked, a drawer opens up that should display historical data when the search attributes are selected.
  • For this purpose, I implemented the fetchHistorySportsDatafromURL method in the actions.
  • Subsequently, I invoked the fetchHistorySportsDatafromURL method within the tabs of the drawer.
  • Unfortunately, the data isn't being rendered as expected.
  • Upon debugging, I noticed that the const historySports = this.props.historySports; returns undefined.
  • Even though I'm using mapDispatchToProps, my attempts have not been fruitful.
  • I am striving to exhibit my data here while reading it from the API {historySports}.
  • All the rendering code is contained within the file sports-advanced-search.js.
  • Below, I have included my code snippet and a link to the sandbox for reference.

https://codesandbox.io/s/5x02vjjlqp

Actions:

export const fetchHistorySportsDatafromURL = data => {
  return dispatch => {
    if (data != undefined) {
      return axios({
        method: "get",
        url: "https://jsonplaceholder.typicode.com/comments",
        data: data,
        config: { headers: { "Content-Type": "application/json" } }
      })
        .then(function(response) {
          console.log("fetchSportsHistoryData--->", response);
          dispatch(fetchSportsHistoryData(response.data));
        })
        .catch(function(response) {
          dispatch(fetchSportsHistoryData([]));
        });
    }
  };
};

Render Code:

getHistoryData = values => {
    this.props.fetchHistorySportsDatafromURL();
};

render() {
    const { classes } = this.props;
    const { value } = this.state;

    const Sports = this.props.Sports;
    const historySports = this.props.historySports;
    console.log("historySports--->", historySports);
    console.log("this.props--->", this.props);
    console.log("this.state--->", this.state);
}

const mapDispatchToProps = dispatch => {
  return {
    fetchHistorySportsDatafromURL: () => {
      dispatch(fetchHistorySportsDatafromURL());
    }
  };
};

export default withStyles(styles)(
  connect(
    null,
    mapDispatchToProps
  )(ScrollableTabsButtonAuto)
);

Answer №1

There are a few issues at play here. Let's try to untangle them a bit without completely solving them for you...

  1. It's important to differentiate between state and props in React. Utilizing Redux to easily map state to props with mapStateToProps can streamline your component usage. Make sure to update the Redux state before attempting this.
  2. Remember, the component-level state defined within the constructor using this.state is different from Redux state. You cannot simply add another state object and expect it to be updated effectively with setState. The state should be bound to the class properly.
  3. Your approach of dispatching
    fetchSportsHistoryData(response.data)
    to update the Redux state seems on the right path. Ensure that there is a reducer listening for FETCH_HISTORY_Sports which will handle updating the Redux state accordingly.
  4. Once the redux state is updated correctly, harness the power of mapStateToProps in your component. This will allow you to access the updated data via this.props.historySports.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Hide custom video controls from view

For a while, I was able to hide the controls on my page using position:absolute; and bottom:-36px, which made them pop up when hovering over the player. Recently, however, I noticed that I can now scroll down to see them. How can I maintain the slide-up ef ...

Personalized Dropdown Menus for Internet Explorer 8

Seeking recommendations for stylish custom select boxes that are compatible with IE8 and function flawlessly. Many of the custom scripts I've come across perform admirably, but tend to suffer setbacks when it comes to working smoothly in IE8. ...

Error in ReactJS VSCode while integrating Cypress testing: The name 'cy' cannot be found

Currently working on a ReactJS Typescript project using NPM and VSCode. Despite all my Cypress tests running smoothly, I am encountering a syntax error in VSCode: Error: Cannot find name 'cy' Any ideas on how to resolve this issue? Attempted th ...

Center Text Field to Linear Progress in React

I'm struggling to position a TextField/Autocomplete with a loader/linear progress at the bottom without it pushing the TextField/Autocomplete upwards. Can anyone suggest a proper solution for this issue? For reference, you can check out the Codesandb ...

Blending synchronous and asynchronous testing with Mocha

There is a function that calculates certain values and informs the user about events using callbacks: function returnAndCallback(callback) { callback(5); // not always called return 3; } Incorporating Mocha and Should.js, a test was created: descri ...

What is the best way to implement a personalized hook in React that will return the number of times a specific key is pressed?

I'm working on a custom hook that should give me the key pressed, but I've noticed that when I press the same key more than twice, it only registers twice. Here's my code: import { useEffect, useState } from "react" function useKe ...

Leveraging AngularJS for a Windows store app

After attempting to integrate AngularJS into my Windows store application, I came across a few recommended solutions: Unfortunately, these solutions did not work as expected. While I didn't encounter the Unable to add dynamic content error, AngularJS ...

What is the best method for passing a JavaScript object to PHP using Ajax?

I have looked into similar questions like this and this, but none of them have helped me solve my issue. When I check the console log for my data, it displays the following: Object["row_LM#00000010", "row_LM#00000002", "row_LM#00000009", "row_LM#00000008" ...

Indicate the node middleware without relying on the port number

My website is hosted at mywebsite.com and I am using node and express for middleware services, pointing to mysite.com:3011/api. The website is hosted statically on Ubuntu 16 (Linux) and the middleware is run separately using pm2 (node server). I want to b ...

Adjust the position of a textarea on an image using a slider

Currently, I am exploring how to creatively position a textarea on an image. The idea is to overlay the text on the image within the textarea and then use a slider to adjust the vertical position of the text as a percentage of the image height. For instanc ...

Enable strict mode for older web browsers

I would like to incorporate the "use strict"; statement into my function, but unfortunately it is not compatible with older browsers such as ie7 and ie8. Is there a workaround to ensure this functionality works in legacy browsers? Could someone please cla ...

When the SVG icon is clicked, it will redirect to a different page

I'm trying to figure out how to add a href attribute to an SVG icon in order to redirect to another page when the icon is clicked. I've saved the SVG icon as a React Component, but I'm unsure of what steps to take next. import React from &ap ...

Creating Browser Extensions with Vue.js and Vue CLI

I am in the process of creating a Chrome Extension with a frontend powered by Vue.js. Everything was going smoothly using vuecli until my app started utilizing the Webextension-API. This API is only accessible to registered Extensions, not normal websites. ...

Retrieve information from an array within a blockchain smart contract

My smart contract factory has the ability to deploy another smart contract. Within the factory contract, there is a specific function: address[] public migrations; ... function getMigrations() public view returns (address[] memory) { return migr ...

Thymeleaf is responsible for fetching the static resources by referencing the REST URI

I am currently utilizing thymeleaf as my chosen template engine for a spring boot project. Here is the directory structure: src/main/ |- java | - UserAdministrationController.java |- resources | - static | - admin | - css ...

Importing ReactDOM alone does not result in the rendering of anything

Having just started delving into the world of React, I've been grappling with getting a React app up and running. Despite my efforts, all I can manage to see is a blank page. Can anyone offer some assistance? HTML Markup (index.html) <html> & ...

Is there a way to modify the styling for ListItemButton specifically when it is in a selected state?

My first experience with MUI involves trying to customize the styling of a button. After installing my project with default emotion as the styling engine, I attempted to override the existing styles using the css() method mentioned in the documentation: ...

Is there a more efficient method for converting an array of objects?

Is there a more efficient way to change just one value in an array without iterating through every element? I've included the code below where I am trying to update the contact number for each user in an array. Although my current solution works, it ...

Attributes for 'v-bind' directives must have a value specified

Struggling to implement a tree structure in vue.js and encountering an issue with element props. Any assistance would be greatly appreciated. I've attempted using :content="{{tempCont}}" as well as content="{{tempCont}}", but neither approach proved ...

Guide on how to restrict specific days and dates in MUI React datepicker using a JSON array

I am working on a Laravel application that generates a JSON response containing dates and days of the week that need to be disabled in the datepicker. For instance, here is an example of my datesOff constant when I log it: ['2022-05-08', '2 ...