0

I'm using react functional components inside that, I have two different array objects. I would like to render the array objects based on the condition which I have. I'm using the function hooks to define a reference and assign to it dynamically. But, this is returning null. can someone help me on this ? In the below code isDemandGraph is returning null while rendering.

     function gridHeatMap(props) {
          const heatMap = [
            { color: 'redVeryHigh', value: 50 },
            { color: 'redHigh', value: 25 },
            { color: 'redMedium', value: 20 },
            { color: 'redLow', value: 15 },
            { color: 'white', value: 0 },
            { color: 'greenLow', value: 15 },
            { color: 'greenMedium', value: 20 },
            { color: 'greenHigh', value: 25 },
            { color: 'greenVeryHigh', value: 50 },
          ];
        
          const demandHeatMap = heatMap.reverse();
        
          const isDemandGraph = useRef(null);
        
          const { height, title, viewName } = props;
          const classes = useStyles();
        
          return (
            <div className={classes.root}>
              <div className={classes.title}>{title}</div>
              <Grid container spacing={0}>
                {viewName === 'demand' ? { isDemandGraph: heatMap } : { isDemandGraph: demandHeatMap }}
                {isDemandGraph.map((item, index) => {
                  return (
                    <Grid item style={{ flexGrow: '1', height, width: '11%' }} className={item.color}>
                    </Grid>
               </Grid>
            </div>
)
12
  • You initialize it to null and never touch it again, it's unclear what you're expecting. Commented Apr 27, 2021 at 14:28
  • @DaveNewton: {viewName === 'demand' ? { isDemandGraph: heatMap } : { isDemandGraph: demandHeatMap }}. In this statement. I'm updating the const value with the array which I need based on condition.And using that to populate on Grid in very next statement {isDemandGraph.map((item, index) => { Commented Apr 27, 2021 at 14:30
  • @TechGeek is not the right way to update. You are returning newly created objects. And not updating isDemandGraph value. Also why to create a ref? Commented Apr 27, 2021 at 14:31
  • @Medi: Is there any alternative way to update that variable ? Commented Apr 27, 2021 at 14:32
  • 2
    You can make isDemandGraph a local state variable and update it with a useEffect() hooks triggered by viewName Commented Apr 27, 2021 at 14:34

3 Answers 3

1

It won't work that way, calling the isDemandGraph inside the ternary operator does not assign value to the same. Also, you should use useState instead of useRef for your usecase. I am adding the updated code snippet, it should work as expected:

 function gridHeatMap(props) {
          const heatMap = [
            { color: 'redVeryHigh', value: 50 },
            { color: 'redHigh', value: 25 },
            { color: 'redMedium', value: 20 },
            { color: 'redLow', value: 15 },
            { color: 'white', value: 0 },
            { color: 'greenLow', value: 15 },
            { color: 'greenMedium', value: 20 },
            { color: 'greenHigh', value: 25 },
            { color: 'greenVeryHigh', value: 50 },
          ];
        
          const demandHeatMap = heatMap.reverse();
        
          const { height, title, viewName } = props;
          const [isDemandGraph, setIsDemandGraph ] = useState(viewName === 'demand' ? heatMap : demandHeatMap);
          // setIsDemandGraph can be used to update the value of `isDemandGraph`
          
          const classes = useStyles();
        
          return (
            <div className={classes.root}>
              <div className={classes.title}>{title}</div>
              <Grid container spacing={0}>
                {isDemandGraph.map((item, index) => {
                  return (
                    <Grid item style={{ flexGrow: '1', height, width: '11%' }} className={item.color}>
                    </Grid>
            </div>
Sign up to request clarification or add additional context in comments.

Comments

1

I suggest you to use useState instead of useRef and use useEffect to listen prop changes like:

function gridHeatMap(props) {
          const { height, title, viewName } = props;
          const [isDemandGraph, setisDemandGraph] = useState([]);
          const classes = useStyles();

          const heatMap = [
            { color: 'redVeryHigh', value: 50 },
            { color: 'redHigh', value: 25 },
            { color: 'redMedium', value: 20 },
            { color: 'redLow', value: 15 },
            { color: 'white', value: 0 },
            { color: 'greenLow', value: 15 },
            { color: 'greenMedium', value: 20 },
            { color: 'greenHigh', value: 25 },
            { color: 'greenVeryHigh', value: 50 },
          ];
        
          const demandHeatMap = heatMap.reverse();
        
          useEffect(() => {
             viewName === 'demand' ?  setisDemandGraph(heatMap) : setisDemandGraph(demandHeatMap);
          }, [viewName]);
        
          return (
            <div className={classes.root}>
              <div className={classes.title}>{title}</div>
              <Grid container spacing={0}>
                {isDemandGraph.map((item, index) => {
                  return (
                    <Grid item style={{ flexGrow: '1', height, width: '11%' }} className={item.color}>
                    </Grid>
            </div>
)

Comments

1

I think this is how I would go (please notice I modified a bit the returned JSX, as the one in your code did not look correct to me, so you might want to double check it):

import React, { useState, useMemo, useEffect } from 'react';
// ... other imports

function gridHeatMap({ height, title, viewName }) {
  const heatMap = useMemo(() => ([
    { color: 'redVeryHigh', value: 50 },
    { color: 'redHigh', value: 25 },
    { color: 'redMedium', value: 20 },
    { color: 'redLow', value: 15 },
    { color: 'white', value: 0 },
    { color: 'greenLow', value: 15 },
    { color: 'greenMedium', value: 20 },
    { color: 'greenHigh', value: 25 },
    { color: 'greenVeryHigh', value: 50 },
  ]), []);
  
  const [currentHeatMap, setCurrentHeatMap] = useState(heatMap);
  
  useEffect(() => {
    viewName === 'demand'
      ? setCurrentHeatMap(heatMap)
      : setCurrentHeatMap(heatMap.reverse());
  }, [viewName]);
  
  const classes = useStyles();
  
  return (
    <div className={classes.root}>
      <div className={classes.title}>{title}</div>
      
      <GridContainer spacing={0}>
        {
          currentHeatMap.map((item, index) => (
            <Grid
              key={`grid-${index}-${item.color}`}
              className={item.color}
              style={{ flexGrow: '1', height, width: '11%' }}
              item
            />
          ));
        }
      </GridContainer>
    </div>
  );
}

export default gridHeatMap;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.