Comments (11)
@vvo Sorry, I interpreted your response wrong. I'll try to provide some more details and a possible solution during the weekend, when I have the time for that.
In short, my solution is to change your componentDidUpdate
handling that always re-created the slider to:
- check if new settings were received
- if anything changed, check if any of the changes require re-creating the slider
- otherwise just apply changes via updateOptions/re-attach events/set handle values
from react-nouislider.
Hi @james-dowell, thanks for reporting this.
Could you first maybe have a look at http://react-component.github.io/slider/ which is a more React friendly slider than react-nouislider?
Then if you really want to use react-nouislider, maybe you could dig more into the issue because we never encountered such issue.
from react-nouislider.
Hi @vvo,
Thanks for getting back to me :)
Yeah, I'm not sure the http://react-component.github.io/slider/ is going to cut it but thanks for the suggestion.
I'll do some more investigation into it and just have a play to see if I can get it to work. If you have any thoughts or pointers in the meantime let me know, likewise I'll report back if I get to the bottom of the issue or get a workaround.
All the best!
from react-nouislider.
Okay, so I have done something rather horrible to get it working.. not sure I can leave it like this but for clarity's sake I'll post my findings.
Forgetting the binding of this
, if I force the component to not update then I have the desired effect. It appears as if the callback I pass down for either 'onChange' or 'onUpdate' is invoked when the slider is rendered. which triggers my update method to run. Calling onChange method on redux-form (somehow - not sure about this yet) then triggers a re-render of my component.. which creates a circular dependency.
In some ways forcing the component to not update makes sense as I want nouislider to handle the changing and controlling of the slider but it feels really wrong and dangerous to have something like the following:
shouldComponentUpdate() {
return false;
}
Not sure if that has sparked an idea for anyone, or if anyone has a better solution but I'm all ears.
Cheers,
from react-nouislider.
Steps:
- Add onUpdate() handler function in a parent component, that modifies that component's state
- Create a slider, and give it that onUpdate handler
- As per noUiSlider docs - onUpdate is called instantly, when attached
- onUpdate() modifies the parent components state, and react calls it's render function
- In React the parent's render causes the child (slider) to render, which in your wrapper is triggering the code listed below
- The slider is destroyed and re-created, thus calling it's onUpdate() again, which repeats the whole cycle
componentDidUpdate() {
if (this.props.disabled) this.sliderContainer.setAttribute('disabled', true);
else this.sliderContainer.removeAttribute('disabled');
this.slider.destroy();
this.createSlider();
}
Realistically, what you probably want to do is to replace componentDidUpdate() with componentWillReceiveProps() and check if there are changes that actually require re-creating the slider.
Additionally, seems like noUiSlider provides an API to update it's settings without destroying it - http://refreshless.com/nouislider/more/#section-update
In general, calling renders very often is normal React behavior, so you definitely do not want to to assign such heavy operations as re-creating something to a handler that could be called many times per second.
Edit: It would also help with #23 (comment)
from react-nouislider.
I am not sure if we as react-nouislider need to take actions on the issues you are facing, if you feel like we need, then open a PR and explain it. Thanks!
from react-nouislider.
I am not sure if we as react-nouislider need to take actions on the issues you are facing
Allow me to disagree, your component pretty much guarantees a crash (with call stack exceeded) if it's passed any update handler, which works with the parent component's state (which is very common practice in react)
E.g. roughly like this, if you want a label showing the current value:
onUpdate(value) {
this.setState({value: value});
}
render() {
return(
<div>
<Slider onUpdate={this.onUpdate}/>
<label>{this.state.value}</label>
</div>
)
}
I'll consider opening a PR, since I'm working on resolving that issue anyway.
from react-nouislider.
@darkalor To be clear when I said "I am not sure" I did not meant "I doubt you are saying the right things".
On this thread I am lost because you guys are far more comfortable with React than me. I will follow and read any of you valuable advice regarding how we need to update react-nouislider if that's the case.
Thanks!
from react-nouislider.
Sorry, have been busy the last week. I have the thing working, but just didn't get the time to make a proper clean PR
from react-nouislider.
Really not managing to deal with this for now.
Here's a gist (not properly cleaned up or anything) of what I'm using in my project, take a look if you can.
Note that I'm relying on lodash for working with arrays/objects.
https://gist.github.com/darkalor/12edbb03a347bf8b99b3135ae1daba5e
from react-nouislider.
Sorry we are not maintaining this repository as much as we should. I recommend moving to https://github.com/airbnb/rheostat
from react-nouislider.
Related Issues (20)
- How to get value? HOT 12
- Import name HOT 2
- Disabled slider support HOT 1
- Error when implementing inside another component HOT 2
- "Behavior" attribute not passing into slider HOT 2
- Does not support server-side rendering HOT 3
- Formatting of tooltips HOT 8
- Using "Tooltips" is giving Warning in console HOT 1
- Flickering on re-render HOT 4
- Getting the end event HOT 4
- tooltip overlays handle after 2 handles HOT 1
- Passing connect array fail HOT 3
- Connect PropTypes have changed HOT 2
- nouislider needs to be updated HOT 9
- connect not showing? HOT 1
- Sliders getting stuck on the edge. HOT 1
- onStart fires multiple times when sliding, resulting touch stuck HOT 1
- Not working with React 16 due to use of propTypes from React, need to use 'prop-types' module to make it compatible HOT 5
- Remove decimals? HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from react-nouislider.