How can I reuse variable values across components in react

LOTUSMS

I thought this would be like setting up a prop but as I understand it, a prop value is managed at the receiving component of the prop. I want to export the value to other components

Here is an example, consider this re-usable component:

RegionButtons.jsx file

import React, { useState } from 'react'
import { useSelector } from 'react-redux';
import { Box, Fab } from '@mui/material'

const RegionButtons = (
  {
    handleNationals, 
    handleState, 
    handleRegionals, 
    handleLocal, 
    selectedIndex
  }) => {

  const [selectedRegion, setSelectedRegion] = useState("local");

  const data = useSelector((state) => state.level)

  const wrestling_level = useSelector((state) => data.wrestling_level[selectedIndex])

  const nationwide = wrestling_level.Nationwide.find((s) => s.type === "Nationals");
  const statewide = wrestling_level.region.find((s) => s.region === "State");
  const regionals = wrestling_level.region.find((s) => s.region === "Regionals");
  const local = wrestling_level.region.find((s) => s.region === "Local");


  return (
    <Box className="button-area" > 
      { nationwide && <Fab onClick={handleNationals}>  NATIONALS </Fab> }
      { statewide && <Fab onClick={handleState}>       STATE     </Fab> }
      { regionals && <Fab onClick={handleRegionals}>   REGIONALS  </Fab> }
      { local && <Fab onClick={handleLocal}>           LOCAL   </Fab> }
    </Box>
  )
}

export default RegionButtons

This particular part below, I am rewriting it a lot, because there are other components that also read data from these nested arrays within the API. (mind you, I couldn't just map the info into an array and export it from redux because they are all coming from several places within the API. Or maybe I could, but either way I didn't go that route)

  const [selectedRegion, setSelectedRegion] = useState("local");
  const data = useSelector((state) => state.level)
  const wrestling_level = useSelector((state) => data.wrestling_level[selectedIndex])

  const nationwide = wrestling_level.Nationwide.find((s) => s.type === "Nationals");
  const statewide = wrestling_level.region.find((s) => s.region === "State");
  const regionals = wrestling_level.region.find((s) => s.region === "Regionals");
  const local = wrestling_level.region.find((s) => s.region === "Local");

What I am hoping to do is to store this in one util.js file that I can export and all I have to do is import nationwide, statewide, regionals, local in other components and I would have visibility into these .find().

WHAT HAVE I DONE SO FAR?

Besides a bunch of useless stuff, the closest I've gotten to it is making a utility file like so

util.js

import { useSelector } from 'react-redux';

export function Regions (selectedIndex) {

  const data = useSelector((state) => state.level)

  const wrestling_level = useSelector((state) => data.wrestling_level[selectedIndex])

  const nationwide = wrestling_level.Nationwide.find((s) => s.type === "Nationals");
  const statewide = wrestling_level.region.find((s) => s.region === "State");
  const regionals = wrestling_level.region.find((s) => s.region === "Regionals");
  const local = wrestling_level.region.find((s) => s.region === "Local");
  
  console.log("nationwide from utils file:", nationwide); //this actually works!

  return nationwide, statewide, regionals, local
}

and then in my RegionButtons.jsx file:

import {Regions} from '../../util';


Regions(selectedIndex) //use it like this and I get that console log I threw out from util.js

The problem is, if I comment out the find() lines and expect the values to still be there from my util.js file it hits me with an undefined error

Sorry for the long question. Thanks in advance

M0nst3R

In your solution, although you are calling Regions(selectedIndex), you are not saving its return value anywhere. Therefore, the constants you needed from util.js will not be defined inside your RegionButtons.jsx component.

You can either destructure the results as such:

const { nationwide, statewide, regionals, local } = Regions(selectedIndex);

Or simply save them inside a constant and use them through dot notation as such:

const data = Regions(selectedIndex);
/* Do something with */data.nationwide;

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Can I reuse components across apps?

How can I reuse software components across different devices in EA (Enterprise Architect)?

How can I share variable data across different files in React

How can I reuse HTML attribute values

How can I reuse a variable in typescript?

Reuse React.useCallback() function across multiple components

How can I identify whether missing values across variables come from the same ID variable?

How can I avoid duplicating props across similar Vue components?

How can I use state across multiple components?

How can I make sticky navbar to work across other components?

Share values and events across different components in React

How can I reuse a function for many different ID values?

How can I reuse a variable inside a sub query WHERE clause?

How Can I change the property of components of React?

How can I prevent the unmount in React Components?

How can I style react semantic components?

How can I sort this map of components? react

How can I return a array with React Components?

How do I save variable values across Activities in Android Studio

How can I reuse a component with different props in React?

React JS: How can I export different components based on environment variable?

How can I count replicate values across columns?

How can I list negative values across my dataset?

How can I assign a set of values randomly across the column in SQL?

How to reuse styled-components between different react components?

How can I preserve an environment variable across su -?

How can I use a variable globally in js (across many files)?

How can I persist an ansible variable across ansible roles?

How can I use Global variable across multiple functions in JavaScript?

TOP Ranking

HotTag

Archive