1

Basically, I have a component called Timeperiod which is a dropdown where users will select values such as "today", "yesterday", "last week", etc.

I want to re-use the <Timeperiod> component in a lot of other components and the problem is I wouldn't be able to access which value they chose if I call it in another.

I know an alternative would be to call my other components inside of Timeperiod and passing them properties based on Timeperiod state but wouldn't that mean I need to create multiple Timeperiod like classes?

Would redux or something be a good way to handle these situations?

Here's my codesandbox: https://codesandbox.io/s/ypyoq8zoz

Right now I only have App.js calling Timeperiod, but I will have many more classes calling it too. I want to be able to access the date chosen in App.js while making the Timeperiod class re-usable.

5
  • For this simple structure, I wouldn't say you should use Redux for it. the option you've thought of is the correct way to do it. Commented Nov 8, 2018 at 13:43
  • So, just pass a function(e.g. getDate(...) ), then pass to it whatever values you need from the Timeperiod component. Commented Nov 8, 2018 at 13:44
  • @HasanSh would I be able to do something like this without having to create multiple timeperiod like components? I want to be able to reuse Timeperiod at its current state Commented Nov 8, 2018 at 13:56
  • Yes, for sure. The getDate prop you pass, will belong to where you pass it to the Timeperiod. If that isn't clear, I can make changed on the sandbox you've created! Commented Nov 8, 2018 at 14:08
  • @HasanSh Yes I would really appreciate it if you showed me in the sanbox. The value in Timeperiod that I would like to use in App.js is ActionListTitle which gets set whenever they change the value of the dropdown..so it'll be either "today", "yesterday" or "last week" Commented Nov 8, 2018 at 14:14

3 Answers 3

3

To decide if you need Redux and how much of Redux do you need, you have to consider carefully the whole application you are developing. Redux adds some boilerplate, which might be too much for the benefits you would gain.

You can however solve your problem without Redux.

Without Redux

One of the key ideas under React is called "Lifting state up". This means that if several components need access to the same piece of data, you should definitely move the storage of that piece of data in a component that is a common ancestor of all those components (see https://reactjs.org/docs/lifting-state-up.html). In your case, it should not be responsibility of Timeperiod to keep memory of the selected date, but of App (indeed, the closest common ancestor of all components that need access to the selected period). Timeperiod should take the selected date from app via a property, and notify App when user changes the selected date using an event (again, a prop containing a function).

class App {
  constructor(props) {
    super(props);
    this.state = {
      start: new Date(), // initial value
      end: new Date(),   // initial value
    }
  }
  render() {
    return (
      <Timeperiod 
         start={this.state.start} 
         end={this.state.end}
         onStartChange={newValue => this.setState({ start: newValue })}
         onEndChange={newValue => this.setState({ end: newValue })}
      />
    );
  }
}

With Redux

Redux allows you to keep a global state and access it in special components called Containers. You can put how many containers you want, in any point of the document tree. This seems great, but has several drawbacks:

  • Too many container components degrade performance
  • Having full access to whole state in any point of the tree could create problems if you are not super careful. A component could modify some data it should not be allowed to access, and so on.
  • Redux introduces some boilerplate that might be too much if the application is simple For any update you have to define some actions capable of handling it, create a reducer to produce the next state from the previous state and the action, and connect together all these pieces

Conclusion It really depends on your application whether Redux is good or bad, but if your component hierarchy is not too deep and your app not too complex, vanilla way could be better

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

Comments

1

So, to follow up our comments. Here's how you could approach it:

Edit Date Picker

The Timeperiod component expects a getDate function that expects a title argument. When you render the Timerperiod component, each time it has a separate state and props.

Check out how I rendered more than one(in the app.js), to show that in action.

Comments

1

Using redux what you could do is have a within your state a timePeriod sub state handled by a dedicated reducer which stores the user's choice.

Then each of your TimePeriod component will be hooked to this state using something like

const ConnectedTimePeriod = connect(
  state => state.timePeriod,
  {
    onTimePeriodChoose (timePeriod) {
      return {
        type: "CHOOSE_TIME_PERIOD",
        timePeriod
      }
    }
  }
)(TimePeriod);

Which hydrates your component with the global shared state and provides a function as a prop to update the time period.

The ConnectedTimePeriod can then be used everywhere and will share the global state.

I would suggest you take a look at the react-redux docs for example and details.

2 Comments

thanks for the response. In your opinion, do you think redux would be the best way to handle the scenario I'm in?
Because you mentioned redux I suggested something along this direction but you could also use context for this but maybe you would just be reinventing react-redux. Using context would mean providing a way for children deeply nested to update the context through some function bound to the component rendering the context.Provider component.

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.