Combine three elements to form just two elements

  • I need to consolidate three different components in my tabs into just two.
  • My goal is to eliminate the basketball component and integrate its functionalities into the Sports component itself.
  • The line below is crucial for creating tab content, so I can't remove the pane component. {this.props.children}
  • Essentially, I am enclosing my content with a div tag. Therefore, I am attempting to implement this directly in the calling component.
  • Here is the code snippet: https://jsfiddle.net/9e767txs/76/
class Basketball extends React.Component {
    render () {
    return (
        <div>
        {this.props.children}
      </div>
    );
  }
};

Basketball.propTypes = {
    label: React.PropTypes.string.isRequired,
    //children: React.PropTypes.element.isRequired
};

Answer №1

It's important to clean up the code and reconsider how data is passed initially.

Currently, initial data is set in state.panes:

    this.state = {
        'panes' : [ 
            <div className="sports-tab-content">
                <p className="sports-large-text jumping1 jumping2">
                    1testing here testing here
                </p>
            </div>,
            <div className="sports-tab-content">
                <p className="sports-large-text jumping1 jumping2">
                    1testing here testing here
                </p>
            </div>,
            <div className="sports-tab-content">
                <p className="sports-large-text jumping1 jumping2 jumping3">
                    1testing here testing here
                </p>
            </div>,
            <div className="sports-tab-content">
                <p className="sports-large-text jumping1 jumping2 jumping3 jumping4">
                    1testing here testing here
                </p>
            </div>
        ]
    }

An alternative approach is to wrap all the data under a single parent div (required for React) and pass it to the Sports class:

    // Parent div
        <div>
            <div label="hand" subtitle="swimmings 1 and 2" liClass="sports-setup-ico first-time-active ft-active-tab" className="sports-tab-content">
                <p className="sports-large-text jumping1 jumping2">
                    1testing here testing here
                </p>
            </div>
            <div label="leg" subtitle="Approx. swimming 3" liClass="sports-invest-ico" className="sports-tab-content">
                <p className="sports-large-text jumping1 jumping2">
                    2testing here testing here
                </p>
            </div>
            <div label="stomach" subtitle="Approx. swimming 4" liClass="sports-balance-ico" className="sports-tab-content">
                <p className="sports-large-text jumping1 jumping2 jumping3">
                    3testing here testing here
                </p>
            </div>
            <div label="finger" subtitle="Approx. swimming 5" liClass="sports-perf-ico" className="sports-tab-content">
                <p className="sports-large-text jumping1 jumping2 jumping3 jumping4">
                    4testing here testing here
                </p>
            </div>
          </div>,

I've also added attributes to elements previously set in your top-level render on the Basketball elements:

<Basketball label="hand" subtitle="swimmings 1 and 2" liClass="sports-setup-ico first-time-active ft-active-tab">
    {this.state.panes[0]}
</Basketball>
....

This part can be simplified to just:

 <Sports selected={0} changeContent={this.changeContent}>
     {this.state.panes}
 </Sports>

Now Sports will receive a wrapper div with individual list item content inside. Adjusting one or two lines allows us to access those children elements:

// We passed the content in a wrapper div, get rid of it
var parent = this.props.children;
console.log(parent);
var children = parent.props.children;

...

return (
  <ul className="tabs__labels">
    {children.map(labels.bind(this))}
  </ul>
);

All the Basketball elements are no longer needed.

Check out the Updated Fiddle.

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

Executing a JQuery function when the webpage is loaded

I am attempting to execute some jQuery code when the page loads, but it doesn't seem to be working properly. The goal is to check if a link has already been saved and, if so, display a table with that information. Is this achievable? It should apply t ...

MUI: A Fragment cannot be used as a child for the Menu component

Developed a Menu component with 3 menu items that are conditionally rendered, but encountering an issue where 'MUI: The Menu component doesn't accept a Fragment as a child. Consider providing an array instead.' keeps showing up on the consol ...

Tips for avoiding anti-aliasing of bitmap font on Internet Explorer and Firefox

I am currently experiencing issues with font smoothing when using a basic bitmap font for icons. While the font appears crisp in Chrome, it appears blurry in IE and FF. Do you have any recommendations on how to resolve this problem? You can view a screen ...

Tips for causing additional items to materialize each time a predetermined item is selected

On my main page, I have an object that I want users to click on to reveal other objects one by one. I currently have 5 layers within my index.html file, but I'm unsure of the specific code needed in each layer to ensure that clicking the main page obj ...

The attribute 'y' is not found within the scope of 'DefaultRootState'

In the directory src/reducers/index.tsx, I organize and output all my reducers like so: import counterReducer from '../reducers/counter'; import loggedReducer from '../reducers/isLogged'; import {combineReducers} from 'redux'; ...

Tips for creating a Vue component that triggers the select dropdown to open when the entire div or wrapper is clicked

I have a custom-designed select dropdown with unique symbols for the select arrow. To achieve this look, I've hidden the default select arrow/triangle and placed our symbol on top as an image file. <div class="select-wrapper"> < ...

There seems to be an issue with the fetch HTTP PATCH request when using the /:id suffix. However, the DELETE

I am facing an issue with correctly fetching the http with the trip._id suffix. The DELETE method works perfectly fine. However, when I attempt to use PATCH, I encounter an error stating "http://localhost:4000/api/tracks/undefined 404 (Not Found)". Strange ...

Using JavaScript to pass a variable into a Promise

I am currently working on a project in EmberJS that involves making an Ajax request to fetch information and then resolving based on a specific part of the response that may vary. return new Promise((resolve, reject) => { const { resourceName, i ...

The JavaScript function is unable to fetch information from the controller

I am working with a controller called Map1 that returns a JSON object. Within my scripts folder, there is a .js file named init.js. In this file, I am attempting to call the controller using the following code: $(document).ready(function() { $.getJSO ...

Guide on validating React input fields using custom validation methods

Currently, I am working with React and dynamically creating new input fields while utilizing React-hook-form for validation purposes. My approach involves: Having a dropdown with numbers as options, such as 1, 2, 3, 4 Generating input fields based on the ...

Understanding the functionality of a notification system

Planning to create an admin tasking system utilizing PHP, MySQL, and JavaScript. Curious about how the notification system operates and how it stores real-time data. Are there any good examples of a notification system? ...

Invoking a nested function within an array

I need to trigger a function with a button click. The function I want to call is nested within another function, which is part of an array. demo = { volej: function(param) { (new initChartist).users(param); }, initPickColor: function(){ ...

I'm having trouble with the pull-right feature in Bootstrap -

I am having trouble with the bootstrap class pull-right. Despite applying it, my span is still floating to the left. I am unsure why the pull-right class is not functioning as expected. <!DOCTYPE html> <html lang="cs"> <head> <me ...

Response to peerjs webrtc call

I am currently utilizing a PeerJS server and PeerJS to establish WebRTC calls between 2 peer IDs. The call initiation process looks like this: var call = peer.call($('#callto-id').val(), window.localStream); The call establishment is functioni ...

The response to ajax requests does not appear in Chrome Dev Tools

I'm experiencing an issue with my nodejs application where I encounter a peculiar situation when making ajax requests using jQuery. When I make a redirection in the callback function of the AJAX request, the response in the developer tools appears emp ...

Mui CardContent not displaying transparent background color properly

I recently completed a course that used MUI v4, and I'm now facing challenges with transitioning to v5. Despite my best efforts, I am struggling to replicate all the styles and find myself stuck. This issue relates to my FeaturedMovie.jsx component i ...

Is there a way to adjust the layout of the dynamic card's columns?

Is there a way to display the card with 2 columns for the first card and then another 2 columns for the second card? Right now, it looks like this: https://i.stack.imgur.com/zu5PI.png However, I would like it to look more like this: https://i.stack.imgur ...

Switch up the stylesheet in WordPress when an administrator is signed in

Looking for a way to incorporate conditional PHP code into my theme that will load a different stylesheet when either an admin or a specific user (ID=1) is logged in. Does WordPress have a function that can make this happen? There's a particular div ...

AngularJS - Establishing communication between controller and view

I am facing an issue with the Angularjs view. I seem to be making a mistake somewhere and I can't figure out where the problem lies. I hope someone can assist me with this. The problem is that {{user.login}} (userRepoInfo.html file) is not being call ...

internet explorer causing issues with .html() method

My webpage is encountering an issue with Internet Explorer when using AJAX to retrieve data and the .html() function to change the content of a div. While Firefox, Google Chrome, Safari, and Opera work fine, IE 7, 8, and 9 do not respond to the .html() fu ...