1

I'm going to be using some configuration data that I want shared between several different React components. Instead of coding those config data objects into each component I want to be able to DRY it up into one file/json string and share that object between components.

What's the most sane/best way of doing this?

(I was looking at Mixins and noticed that they're being deprecated in React at some point, so wanted to avoid this. https://facebook.github.io/react/blog/2015/03/10/react-v0.13.html )

What are other possibilities? Passing an obj into via properties? Creating a file and importing it? Other better ways?

Dr. Chocolate
  • 2,043
  • 4
  • 25
  • 45
  • Possible duplicate of [Correct way to share functions between components in React](https://stackoverflow.com/questions/32888728/correct-way-to-share-functions-between-components-in-react) – Dan O Jun 24 '17 at 23:57
  • Dan the issue is that it's not 'functions', it's data. It's config objects that I want to pass around. – Dr. Chocolate Jun 25 '17 at 01:25
  • functions and "data" are both just Javascript objects. The pattern for sharing them amongst React components is going to be the same: most likely they're going to be passed around as props (mentioned here) or imported from an external file (mentioned in the linked question). – Dan O Jun 25 '17 at 21:11

2 Answers2

1

Why won't you create a util file and export the functions from there? Then import the relevent functions where needed

  • That would be great but it's mainly config DATA. There's no functions which is why I was confused and it felt weird to make an config data object into an exports. – Dr. Chocolate Jun 25 '17 at 01:24
  • 1
    You could do a config file and import it as well. Irs pertty common – user8208380 Jun 25 '17 at 08:36
1

You can keep it in state and pass those configuration as default props to every component.

mhdnp1234
  • 178
  • 7