Add a new division to a component when a specific event handler is triggered by another component using React and the reduce method

Currently, I am developing an interactive drag-and-drop application using React and Redux. My goal is to insert a new div element into a container when the ondragstart event handler is triggered by a component.

The component responsible for the dragging operation is structured as follows:

import React, {Component} from 'react'

import {classify} from '../functions/functions'

export default class Actions extends Component {
    constructor(props){
        super(props)
        this.state={opacity : 1};
        this.dragstart = this.dragstart.bind(this)
        this.dragend = this.dragend.bind(this)
    }
    dragstart(e){
            this.setState({opacity : .4});
            let id = e.target.id;
    }
    dragend(){
        this.setState({opacity : 1});
    }
    render(){
        
        let name = classify(this.props.name);
        return  <div className={this.props.class} draggable='true' onDragStart={this.dragstart} onDragEnd={this.dragend} style={this.state}>
                    <img className="default actions-icon" src={'./icons/'+name+'/'+name+'-.svg'}/>
                    <span className="default">{name}</span>
                </div>
    }
}

The component where I intend to append an element:

import React, {Component} from 'react'

import Action from './actions'

class Activity extends component{
     
    render(){
        const initialState = {
            drag: 'Off'
          }
        let Actions = this.props.tasks.map((task)=> <Action key={task.name} name={task.name} class='default1 activity'/>)
        return  <div id={this.props.id} className='default1 phase' >
                    {Actions}
                </div>
    }
}

export default Activity

This new element will be a child of the following component:

import React, {Component} from 'react'

import Action from './actions'
//import Activity from './activity'

import {actions} from '../variables/variables'

export default class Actionsbar extends Component {
    render(){
        return <div id='process-display' className='default'>
                    <div id='pase-0' className='default1 phase'>
                        <Action key='debut' name='debut' class='default1 activity'/>
                    </div>
                    <div id='pase-1' className='default1 phase'>
                    </div>
                </div>
        
    }
}

As a newcomer to React and Redux, I'm struggling with maintaining state in the store and connecting it with components. Any help would be greatly appreciated.

Thank you in advance.

Answer №1

I find it strange to save UI state in redux's store. The UI state of a component should be kept in the component's state, and then passed down to its children through props.

Only consider using redux for storing UI state if the components are significantly far apart in the hierarchy. However, it's important to carefully weigh the decision as adding redux can add complexity to the codebase.

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

Clickable link remains functional after being hidden

Even though I have hidden the Games-div using the .hide() function in jQuery, my links are still clickable. Is there a way to make them unclickable after being hidden? Here is the link to my code on jsFiddle: http://jsfiddle.net/hypertje/Frv8G/ Note: The ...

Switch background color multiple times on click using JavaScript loop

Hello amazing people! I have a container with 52 small boxes and one large box containing a letter. The smaller boxes are arranged around the larger one using CSS grid, and when hovered over, they turn light grey. My Objective When any of the 52 small b ...

Python Selenium: How to locate elements using xpath in the presence of duplicate elements within the HTML code

Currently, I am utilizing selenium to extract data from a liquor sales website to streamline the process of adding product information to a spreadsheet. My workflow involves logging into the website using selenium and searching for the specific product. Wh ...

What is the best way to ensure that my grid remains contained within its designated area?

I need to incorporate a grid of 16x16 or 64x64 into my layout without it overflowing. I'm considering using flexbox, but unsure of the implementation process. My goal is to have the grid resize based on the container size rather than exceeding its bou ...

Retrieving a subset of an array column in Sequelize: A comprehensive guide

I am currently working with a table that has an array column named tokens When querying this table using npm sequelize, I sometimes encounter an issue where the tokens column contains up to 20k elements, even though I only need 10 elements from it. In st ...

When the user clicks, I plan to switch the audio source

I am looking to update the audio source when a button is clicked, but I am having trouble getting it to work. image description data() { return { audioSrc: '' } }, methods: { setActiveAudio(item) { this.$refs.audioE ...

Node version 6.11.0 experiencing JavaScript heap error even with zero memory usage in the program

Encountering an out of memory error even though only two variables (i & j) are being used. Can someone please provide clarification? I am not storing anything in memory or saving it to any storage; whatever is generated is outputted to the console and the ...

Utilize AngularJS to enable the forward and back buttons to fetch JSON data, but ensure that the data is only retrieved if the item meets

I have successfully implemented forward and back buttons for paging through data items in an array from a JSON file: Controller: dishControllers.controller('DrinkcardsController', ['$scope','$http','$routeParams', ...

The issue of Angular 6 view failing to display accurate data upon page load

In my Angular 6 application, I've implemented a login feature using Firebase's Google login. The interface includes a button labeled Login when the user is logged out, and changes to Logout with the current email address displayed when the user i ...

The height of the href is not displaying correctly when the text-decoration is disabled and it is placed next to

In my design, I have a link positioned next to an image with perfect vertical alignment. Everything looks great when the text has the standard text-decoration, centered vertically and all that. However, as soon as I remove the text-decoration, leaving the ...

I am experiencing difficulties updating my website

Is there a way to automatically refresh my webpage in PHP whenever I press the enter key? ...

Prevent Object Prop Modification in Vue.js

Within the parent component, I initially have an empty filter object like this: {}. The child component, called filter component, is a child of the parent component and here's how it is implemented: <filter-component :filters.sync="filters&q ...

What is preventing my counter from functioning when I click on the canvas?

I am attempting to increment the count every time a bouncing ball in the browser is clicked using this.setState({count: this.state.count + 1});. I thought my code was correct since I have done similar tasks before without using canvas, but it's not fu ...

Activate a dropdown menu following the selection of a date on a calendar input using vue.js

What I need to do is have two select fields, one for 'days' and the other for 'hours'. When a day is selected, the user should then be able to choose between two available time slots. If they choose day two, the available time slots sh ...

Combine identical arrays of object keys into one unified array

https://i.sstatic.net/A2r5c.png I am striving for this particular output [ productId:106290, productserialno:[{ "12121", "212121" }] ] ...

"Using Javascript to assign a class based on a date being greater than

I am facing an issue with a script that applies a CSS class to table cells if the date is greater than a certain value. Currently, the script only works for today's date. I need it to work for dates within and outside of this week as well. $('td ...

Verify if there are duplicate values present in a table

I have a Table that contains multiple rows, each row having input fields. I need to check for duplicate values within the table. Below is my code where I am currently checking for empty values, how can I modify it to also detect duplicate values? JavaScr ...

Issue with mapStateToProps not reflecting changes in props after localStorage modification

Currently, I am working on a redux application that involves authentication. My main concern right now is ensuring that the user remains logged in whenever they interact with the app. Below is a snippet from the bottom of my App.jsx file: function mapStat ...

Utilizing React Native Eject feature while retaining compatibility with React Native code

I have experimented with small prototypes using Expo and React Native, but now I am working on a more complex project where I need to eject in order for certain components to function properly. My concern is, after ejecting, will I be able to continue wri ...

The problem with Text Input and Label Effects

This code is currently in use. It seems to be working fine, but there's one issue that needs addressing: when moving from one input field to the next, the labels for each field are getting mixed up with their corresponding inputs. This problem will b ...