Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Local State #8

Closed
curran opened this issue Feb 27, 2017 · 0 comments
Closed

Local State #8

curran opened this issue Feb 27, 2017 · 0 comments

Comments

@curran
Copy link
Owner

curran commented Feb 27, 2017

In most cases, it makes sense to use unidirectional data flow like Redux to manage state at the root of a component tree (e.g. a stopwatch app). However, in certain cases it's useful to have local component state. In the React world, the utility of local state has been proven time and again, see this thread Question: How to choose between Redux's store and React's state? #1287.

For example a spinner. A spinner needs to use a timer to re-render itself as it spins. Also, the spinner is the only thing that will be using this timer, and its time elapsed (it "owns the datum"), so it doesn't make sense to put it in the state of the Redux store.

Therefore this library needs to have local state, similar to React's local state. Therefore it also needs to have lifecycle hooks, create and destroy.

Here's an API sketch for what local state could look like.

var spinner = d3.component("svg")
  .create(function (setState){
    setState({
      timer: d3.timer(function (elapsed){
        setState({
          elapsed: elapsed
        });
      })
    });
  })
  .render(function (selection, props, state){
    selection.call(spinnerWheel)
      .select("g")
        .attr("transform", "rotate(" + state.elapsed*props.speed + ")");
  }
  .destroy(function(state){
    state.timer.stop();
  });

More motivation for renaming d to props.

@curran curran closed this as completed in 1815fd7 Feb 28, 2017
@curran curran mentioned this issue Feb 28, 2017
3 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant