0

I am following this: Merge two JSON data into one with particular key values

Now, I added an useEffect() hook. How can I make use of useRef to store my result? This is an asynchronous function.

const App = (props) =>{
    let result = useRef ()
    useEffect(()=>{
      return()=>
        (result.current = [courtdata, balldata].reduce((p, q) => p.map((c, i) => Object.assign({}, 
        {[c.court_id] : q[i].color}))))
      }.[someTrigger])
   return(
        <ul>
           {result.current.map(r => (
           <li>
             Court id - {r.court_id} | Ball colour - {r.color}
           </li>
      ))}
    </ul>
)
}

Now my {r.court_id} and {r.color} are empty.

Things I have tried:

const App = (props) =>{
       [result, setResult] = useState({})
        useEffect(()=>{
          setResult([courtdata, balldata].reduce((p, q) => p.map((c, i) => Object.assign({}, 
            {[c.court_id] : q[i].color}))))
          }))
       return(
            <ul>
               {result.map(r => (
               <li>
                 Court id - {r.court_id} | Ball colour - {r.color}
               </li>
          ))}
        </ul>
)
}

error: TypeError: result.map is not a function

2 Answers 2

1

As per your render statement, resolve data like below

If you choose useState option

 useEffect(() => {
    const final = [courtdata, balldata].reduce((p, q) => p.map((c, i) => Object.assign({}, c, {color: q[i].color})));
    setResult(final);
  }, []);


Example https://codesandbox.io/s/react-hooks-usestate-shz9v

Sign up to request clarification or add additional context in comments.

7 Comments

why is it that when i removed useEffect, i will have an error that states Error: Too many re-renders. React limits the number of renders to prevent an infinite loop.?
hey, there is an issue here. the the const final statement is always re-rendering and it's causing my application to hang
Updated Answer.. Now it will render once only
It will render only once just like ComponentDidMount
why does removing that works? i thought the property of useEffect() is if something changes, it will re-render. but why does it keep re-rendering even when there are no changes? And how do the application know when to re-render since it has been removed?
|
0

useEffect hook execute after component mounted, and useRef is to access some dom component.

I think what you need is useState.

const [result, setResult] = useState({});

4 Comments

I have tried that and it seems that I have an error. I have edited my question
result.map suppose the result is an array, you need to do const [result, setResult] = useState([]);, useEffect will execute later then the component mount
i do not have error anymore, but my output is still null. Probably because the result inside my useEffect() isn't captured
Try to use react devtool to see if the state has been updated.

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.