I am using Redux for state management, my state is an array of objects. It looks like this: I do not care for it being an array, I just need the ability to display object values by a key since there are many objects and each needs to displayed in a specific area.
[{...},{...},{...},{...},etc..]
How can I best display values of a specific object by it's key?
Reducer:
const contextReducer = (state=null, action) => {
switch(action.type){
case 'CONTEXTUALIZE':
state = Object.values(action.payload)
state = state.filter(o => (o === state[0] || o === state[3]))
state = Object.assign(...state)
state = Object.keys(state).map(key => ({[key]: state[key]}))
console.log(state)
return state
default:
return state
}
}
export default contextReducer
My attempt:
const context = useSelector(state => state.contextReducer)
followed by {context}
in the render method, but I get an error:
Objects are not valid as a React child (found: object with keys {LaserHost}). If you meant to render a collection of children, use an array instead.
I am able to instead use {context.toString()}
but this just displays the entire array of the string 'object' and not the actual keys and values of each object in the array.
Render Method
export default function ContextPanel() {
const classes = useStyles();
const context = useSelector(state => state.contextReducer)on={<ExpandMoreIcon />}
aria-controls="panel1a-content"
id="panel1a-header"
>
<div>
<Typography className={classes.heading}>{context}</Typography>
</div>
<div className={classes.nameColumn}>
<Typography className={classes.heading}>{context}</Typography>
</div>
<div>
<Typography className={classes.heading}>{context}</Typography>
</div>
</ExpansionPanelSummary>
<ExpansionPanelDetails>
<Typography>
{context}
</Typography>
</ExpansionPanelDetails>
</ExpansionPanel>
Everywhere you see {context}
, I intend to be able to access a value by a key of an object. I do not actually want to display all of {context}
. This is just a placeholder for now.