Organizing components that display slightly more intricate data: Choosing between storing data, passing props, or combining into a single component

I am looking to develop a component that enables users to edit data for a mathematical classification model.

The interface concept may resemble the following:

interface MathModel {
  name: string;
  article: ArticleDescription;
  criteria: Criteria;
  coefficients: Coefficient[];
  interpretation: ResultInterpretation;
}

interface Coefficient {
   value: number; // not equal to 0! how to validate?
}

interface Interpretation
{
   description: string
   interpretationItems: InterpretationItem[]
   // ...
}

interface InterpretationItem {
  scoreMin: number
  scoreMax: number
}

In my initial attempt, I tried creating separate components to manage each property of MathModel. For example: MathModelCoefficientList, MathModelInterpretation components.

However, the interpretation component requires additional information that is not included in the Interpretation type. It requires knowledge of the sum of all coefficient values. This is because InterpretationItem scoreMax should not exceed the total sum of coefficients.

Additionally, InterpretationItems are interconnected - each InterpretationItem should represent a distinct score range.

Therefore, simple v-model components cannot be used.

Potential solutions:

  1. Within MathModelComponent, create a computed variable
const coefficientsValuesSum = computed( () => ...)

const nonOverlappingInterpretationItems = ...

then

<MathModelInterpretation v-model=modelValue.interpretation :coefficientsValuesSum=coefficientsValuesSum :nonOverlappingInterpretationItems=nonOverlappingInterpretationItems>

<MathModelInterpretationList v-model=modelValue.interpretationItems :coefficientsValuesSum=coefficientsValuesSum :nonOverlappingInterpretationItems=nonOverlappingInterpretationItems>

<MathModelInterpretationItem v-model=modelValue[i] :coefficientsValuesSum=coefficientsValuesSum :nonOverlappingInterpretationItems=nonOverlappingInterpretationItems[i]>

While it allows us to see dependencies of subcomponents, handling numerous props raises concerns.

  1. Create a store containing MathModel. Dependencies are less explicit. Moreover, is it acceptable to pass an index to a component which then retrieves data from the store?

<MathModelInterpretationItem :interpretationItemIndex=i>

  1. It's not such a complicated component. Avoid dividing it. Maintain Everything in a Single Component.

Extra:

Extra 1) Validation. After splitting into subcomponents, validating before submission becomes more challenging, correct?

Extra 2) I could construct a TypeScript class MathModel with a method sumAllCoefficientValues(). How can TypeScript classes and their methods connect with the Vue reactivity system? Does this approach make sense?

Answer â„–1

Special thanks to Estus Flask for helping clarify the issue with mutating object properties and arrays in a trap-like situation.

I am still searching for the best approach to creating a component that manages a list of objects. Let's imagine a scenario where we have a list of coefficients with various complexities:

Let's review the MathModel type:

interface MathModel {
  coefficients: Coefficient[];
  //...
}

interface Coefficient {
   value: number;
   x: number;
   y: number;
   yetAnotherProperty: SomeObject;
}

In our MathModel.vue file:

<template>
 ...
 <CoefficientsList :mathModel.coefficients >
<template>

So, how can the CoefficientsList inform the parent MathModel component about changes to a property within the passed array? And how can we ensure immutability of each element within an array containing nested objects?

One solution could involve CoefficientsList making a deep copy of the prop array and emitting an event with a callback containing the updated deep copy. We could even incorporate the usage of v-model:

<CoefficientsList v-model:mathModel.coefficients>

This approach requires creating a deep copy and passing potentially large data payloads in callbacks. Additionally, we would need to monitor any changes within the deep copy to emit an update:modelValue event. Are there other considerations to keep in mind?

Alternatively, I have contemplated allowing the parent component to mutate its state by receiving events such as: elementChange(newElement, index), elementRemoved(index).

However, given the nested structure of the new Coefficient type, it seems likely that deep copying will still be necessary—especially if we intend to use v-model on

coefficient[index].yetAnotherProperty
to link it to another component like YetAnotherComponent.

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

Retrieve the thousand separator for numbers using Angular in various languages

When using the English locale, numbers appear as follows: 111,111,222.00, with a comma as the thousand separator and a point as the decimal separator. In languages like German, the same number would be represented as 111.111.222,00, reversing the positions ...

AngularFirestore: It seems that the 'id' property is not available on the 'QueryDocumentSnapshot<any>' type

Encountering a challenge with Angular5 and Firestore My Objective Retrieve a Firestore Collection named minutes Assign the content of the collection to my component variable, also named minutes, as an Observable object containing the ID of each minu ...

The JSX component is unable to utilize the object

When working with Typescript in a react-three-fiber scene, I encountered an error that GroundLoadTextures cannot be used as a JSX component. My aim is to create a texture loader component that loads textures for use in other components. The issue arises f ...

Obtaining a string union value dynamically during execution

Question 1: Exploring a component library, material-ui, which offers interfaces and types for customizing the css class values of each component. For the Select component, they define a type as a combination of string literals type SelectClassKey = " ...

What is the best way to transform private variables in ReactJS into TypeScript?

During my conversion of ReactJS code to TypeScript, I encountered a scenario where private variables were being declared in the React code. However, when I converted the .jsx file to .tsx, I started receiving errors like: Property '_element' d ...

Angular2 - error in long-stack-trace-zone.js at line 106: Zone variable is not defined

Currently, I am utilizing the Angular2 starter with webpackjs AMD. While there are no build errors showing up, I encounter some issues when browsing (using npm server), resulting in the following errors: What aspects might I be overlooking in my build con ...

How can you use Vue.js with NW.js to seamlessly open a link in the user's preferred browser?

Essentially, the question lies within the title itself, but allow me to elaborate. I have a proposed solution for this issue, however, I believe it lacks practicality and I am seeking a more efficient approach. For instance, opening one link is not a prob ...

The inconsistency between the overload signature and the function implementation is illogical

I'm encountering an issue with my TypeScript code that I believe should be throwing an error: export class Alpha { getA(alpha: string | number); getA(alpha: number) { const beta = alpha * 2; console.log(beta); } } const a = new Alpha(); ...

Learn how to retrieve data prior to rendering with Vue 3 and the composition api

Is there a way to fetch data from an API and populate my store (such as user info) before the entire page and components load? I have been struggling to find a solution. I recently came across the beforeRouteEnter method that can be used with the options ...

There seems to be a complete absence of rendering in this particular vue

Upon initializing a fresh Vue project using Vue CLI 3 and removing all default views and components, I proceeded to add two new views. To my surprise, these views did not render when changing the URL; instead, I was met with a blank page and no error messa ...

implement a data update feature in a Vue application using methods and Axios to manipulate

I am having trouble displaying data on a graph in my web interface using Vue js. I am fetching data from the backend to the frontend with axios, and although I can display an empty graph, the passed data is not showing up. Can someone please help me iden ...

List in Angular remains empty

I am facing an issue with populating a simple HTML list using Angular. The problem arises when trying to display the data in the list. When I use console.log("getUserCollection() Data: ", data);, it shows an array which is fine, but console.log("getUser() ...

Working with Angular: Managing an Array of Objects

After retrieving an array of objects using the code snippet below: this.serviceOne.getRemoteData().subscribe( data => this.MyArray.push(data) ); I encounter issues when trying to iterate through the array using the code snippet bel ...

Upon reacting with Typescript, the window will transition to the homePage, however, it will also reset

Trying to redirect this component to the HomePage</code causes the data to restart once it reaches the home page.</p> <p>Any recommendations for an alternative to <code>window.location.href = "/HomePage"? import React, { useE ...

Utilizing webpack, gulp, and typescript to efficiently incorporate jQuery plugins

I'm having trouble figuring out the correct way to load third-party libraries that have dependencies on each other. I am using TypeScript and Gulp with either Webpack or SystemJS for my module loader, both of which are throwing similar errors in this ...

Leveraging the power of Vue.js by incorporating various instances of pagination

Is there a way to use multiple instances of Vue.js's Pagination plugin within a loop effectively? Consider this example for (let i in sources) { // create a new tab with its own pagination } I am facing an issue where the @page-changed="pageCha ...

What is the best way to showcase a route parameter in the context of Vue.js and Firebase integration

I am currently working on a blog project that offers free sewing patterns as content. My approach involves using route parameters to fetch each blog individually. However, I have encountered an issue where the blog's data is not being retrieved from f ...

The loading spinner isn't appearing while the function is running

Having trouble displaying a loading spinner while a function is running. I've tried different methods, but the spinner just won't appear. Here's the HTML snippet: <div class="row pt-3" id="firstRow"> <div class="col"> <bu ...

Tips for Dealing with Empty Rows in Arrays

How can I remove rows from an array in Alasql where all key values are null? Here is the array data: [ 0:{Name:"ABC1",No:5,BalanceDue:5000,Notes1:null,Notes2:null,CurrencyId:"2",Date:"06/01/2018"} 1:{Name:"ABC2",No:6,BalanceDue:6000,Notes1:null,Notes2: ...

Is there a way to verify the selected navigation item in the console?

Need help with checking the selected navigation using console.log? export const navItems: NavData[] = [ { name: 'Book #1', url: '/book', icon: 'fa fa-book', children: [{ name: 'Book #2', ...