arrays vs key value pairs (Map/Object) in React - reactjs

I am often torn on wether or not should I be using a key value pair data structure or an array in React when representing a collection of uniquely identifiable objects.
For example, say we would like a component to receive a collection of messages. Is there anything wrong with modeling them like this,
{
message1: {from:"bill" , to:"frank"},
message2: {from:"Jill" , to:"sammy"}
}
or similarly as an ES6 map, should we use an array of objects like this
[
{name:"message1" , from:"bill" , to:"frank"},
{name:"message2" , from:"Jill" , to:"sammy"}
]
I would like to know if there is some kind of best practice regarding the use of key value data structures in React.

In my opinion it's better to use objects, even if it's more easier to list/map through the data when using arrays, in most cases you'll want to edit specific elements and an object with the identifier as key will help a lot.
This provides some useful details:
https://www.youtube.com/watch?v=aJxcVidE0I0&t=415s

Related

How can I update arbitrarily/dynamically nested data with immutability helper?

To update nested data with immutability helper, you usually hard-code the path to the data you want to update, sometimes with variables for keys. What do you do if the number of keys/indexes (that is, the depth of nesting) is also variable? I'm looking for a way to update data given an arbitrarily long list of keys. Given [0, 1, 1], I want to update data[0][1][1], or given [9], I'd like to update data[9].
Context: I have a deeply nested array of comments, where a nested comment is a reply to its parent, and I need to add/remove/edit comments based on which comment is selected.
It looks like Immutable.js has methods that work just like I'm describing:
Immutable.JS’s get() or getIn() methods … [access] properties via an array of strings, each of which represents a property key.
https://redux.js.org/recipes/using-immutablejs-with-redux#difficult-to-interoperate-with
Is there a good way to do this with immutability-helper? If not, it's not too late for me to switch to Immutable.js.
The helpers can be used on arbitrary objects, not just immutables. These include getIn, setIn, and updateIn:
Take a look at the docs, they are quite convinient and come with samples.
const { getIn } = require('immutable');
const myDynamicPath = [0, 1, 1];
getIn({ 1: { 2: { 3: 123 }}}, [1, 2, '3'], 'ifNotSet') // 123
PS: Take care when using numbers as keys, as they are always strings on objects. ImmutableJS (and native Map/Set) on the other hand can deal with arbitrary types in keys and are therefore type-strict when used on ImmutableJS collections. So if you ever convert that data object to an Immutable, you might have confusing results if you do not pay attention.

ReactJS - Is it possible to render this dictionary (via map) when the keys can change?

Question Is it possible to render a dictionary with a key (which isn't known until an algorithm is run) with a value that is an array, itself with a dictionary with unknown key-value pairs until an algorithm is run?
Detailed information
I have this dictionary:
var currentWorkers = = {EmployeesAtRestaurant :
[{"James" : "Manager"},
{"Jessica" : "Waiter"},
{"Bob" : "Waiter"},
{"Ben" : "Chef"}],
EmployeesAtOffice :
[{"Rebecca" : "Manager"},
{"Nicole" : "Part-time Employee"},
{"Robert" : "Full-time Employee"},
{"Eric" : "Full-time Employee"}],
EmployeesAtZoo :
[{"Robert" : "Manager"},
{"Naomi" : "Part-time Employee"},
{"Jennifer" : "Full-time Employee"},
{"Ken" : "Full-time Employee"}]}
And I want to render it on a page as below (mock up). It is to display employees of an organisation:
What I've tried
I've read some previous answers on Stack (Push component to array of components - ReactJS) of how to attempt this, but their dictionaries use a simple key and value pair, and since my key is not known (i.e I can't do dictionary.Organisation for example) I'm not able to do the above.
I've tried to remodel the dictionary into a model similar to the above, but then I lose a lot of the information above.
Frankly, I'm beginning to suspect my best option is to just remodel the dictionary at this point, if the above is too difficult to attempt.
To make sure I'm understanding your question: Are you talking about the special prop called key[1] that React uses for rendering?
If that's the case, the important thing is that each key is unique, but it doesn't necessarily have to be the same key that your algorithms are calculating.
If you don't have access to the results of your algorithm yet, but you still want to render the strings like in your screenshots, you'll need a different unique key to use while mapping.
The map function on Arrays sends the element as the first function parameter, and the element's index as the second parameter[2]. Lots of places will warn you that index keys aren't the best. As far as I know, this is because if the order of the Array shifts then you lose the optimization that React is trying to provide you.
If index is the only unique data you've got, however, it's reasonable to consider using it. This is especially true if the data is coming from a static source, because you know that the order of the data isn't going to shift out from under you.
Let me know if I've misunderstood your question.
https://reactjs.org/docs/lists-and-keys.html#keys
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map

How do deal with nested Arrays/objects in BehaviorSubjects, Observables?

I generally have problems using rxjs with nested Objects or Arrays.
My current use-case is this:
{a: [
{b: 0, c:[{d:1}]},
{b: 1, e:[{f: 'someString'}]}
]
Task: Get and set the Observable or value of a,b,c,d,e,f. I also want to be able to subscribe to each property.
I had this Problem in a similar use-case with an Array of BehaviorSubjects:
Efficiently get Observable of an array BehaviorSubjects
I generally have problems to use the basic functionality of nested arrays/objects in rxjs.
The basic functionality I mean includes:
Array:
getting Element by Index
using for of/in on Arrays
setting an Element by Index
push, pop, shift, slice, splice, ...
Object:
getting Value by Property name
going into the nested tree: object.key1.key2.key3[3].key4 ...
setting Value by Property name
assign
for of/in loops
Generally:
Destructuring: e.g.: let [variable1, variable2] = someObject;
Maybe other stuff I forgot.
I dont know if and which functions are possible for which rxjs Objects and which make sense (for example you should be able to set values in an Observable directly). But coming from a background without rxjs, I have trouble to manage my rxjs Objects properly.
I think reason for this besides my lack of knowledge and understanding is, that
a. The rxjs Objects don't provide the functionality as I'm used to from normal arrays and objects. e.g.:
let variable1 = array[1].property;
//becomes this (see related stack-Question I mentioned earlier)
let variable2 = array.pipe(mergeMap(d=> d[index].pipe(map(d1 => d1[property]));
// -> what happens here? You first need to know what mergeMap,
// map is doing and you have 5 levels of nested inline functions.
b. To implement the those mentioned functionalities I need to go over the .pipe() function and use some function like mergeMap, map, pluck, ... Functions that aren't directly indicating that you can get the Observable of let's say 'e' in my example. Making something like object.a[1].e wierd to implement (at least I don't know how to do that yet)
EDIT:
I also want to note, that I still love the idea of rxjs which works well in angular. I just have problems using it to it's full extend, as I'm a bit new to angular and consequently rxjs.
I thin RX is mainly focus on dealing with async operations. Mutation of array and object we can perfectly use the methods comes natively with javascript if theres no existing operators. or you can create your own operator for mutation/iteration etc.
Will try to answer some of your question on array/objects mutation, they are actually very straight forward.
Array:
getting Element by Index
map(arr=>arr[index])
using for of/in on Arrays
map(arr=>arry.map(item=>....))
setting an Element by Index
tap(arr=>arr[index]=somevalue)
Object:
getting Value by Property name
pluck('name')
going into the nested tree: object.key1.key2.key3[3].key4 ...
pluck('key1','key2')
setting Value by Property name
map(obj=>({a:value,obj...}))
assign
lets say your really want some pick array index method as rxjs operator you can create something like, same as for..in operations.
const pluckIndex=(index)=>source=>source.pipe(map(arr=>arr[index]))
const source = of([2,3])
source.pipe(pluckIndex(1)).subscribe(x => console.log(x));

React Native SectionList replace data key

I am fairly new to React and React native, but I have bumped into a problem when populating a SectionList and could not find a solution yet.
I have an array of objects, and each object has it's own array. A SectionList is ideal to display this info, however, the array for each section is not called "data", while SectionList seems to expect the key "data" for the array in each section.
Is there a way to tell the SectionList to use another key instead of the data key to populate each section's array?
As Per my Knowledge on SectionList each Section will Expect a data and Key .
What you can do is modify your Data as per that.
like for Eg :
your data is [[1,2,3],[4,5],[6,7]]
what you can do is iterate through this array and push each subArray into New Array With Format {key:index,data:subArray}
hope this will Help :)

react: Modifying a dictionary inside of a list

I have a state object like
this.state = {
newPerson: '',
people: [{name:'Eric', update: false} , {name:'Rick', update:false}, {name:'Yoni', update:false}]
};
I want to map over the list and be able to modify N object (ie - set status to be true).
I was thinking that I could map over the list of dictionaries by checking to see if the name matches N object's name, then "pop out" / delete the dictionary, modify it and then re-add it.
Is there a better way to do this? Especially following react's "functional" programming style by not modifying a object in space.
You can just map over your people list and modify (merge) only the one that matches the requirement (eg name in your example - it won't affect the original array). But the best way would be indexing your objects in collection and then using map function - you can have duplicated names at some point.
I know it's redux docs, but can help you with your problem - https://redux.js.org/recipes/structuring-reducers/immutable-update-patterns

Resources