-
-
Notifications
You must be signed in to change notification settings - Fork 32.5k
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
[Autocomplete] componentWillReceiveProps method's condition #3359
Comments
I don't agree. Can you use |
What is the intention of searchText? If it is an input to the component, it should not be mutated. |
The
Could we see this change? That will help. |
@oliviertassinari Will do. Today. Thank you so much for being so kind and so patient. I apologize for not being prompt. Deadlines and stuff have taken priority. I would like to add that Material UI is a great library of components. And so far it has been great to work with them and i have been learning a lot! 👏 |
Here are the code changes that i made in my node_modules. I just removed the inequality check, in the https://github.com/sameeri/material-ui/blob/master/src/auto-complete.jsx Here is the
So let's say the app has a set of super heroes and based on some super hero searched via the Auto Complete component, we display the superhero's characteristics, maybe using our Card component. And upon some particular user actions on the page, there is a necessity of starting with a completely blank slate. That includes the clear of our Auto Complete's searchText. Since we are using the container + presentation component separation and data flows into the component. I expected the Auto Complete component to honor the searchText input property and clear the searchText, upon re-rendering. Just like any other component. This is however not the behavior of the component. So i dug into the code. And these are my notes from the code & the observed behavior.
Since i am treating it as a presentation component, the props will always be the same, the current code sees the this.props and nextProps as the same thing and it won't honor the property specified and state will not update. The method you have specified 'onUpdateInput ' fires on each key stroke if i remember the behavior correctly. So i don't know if it is a suitable method. To better understand my use of components, and predict the behavior of the component, i need to understand the component's intentions. I had made changes, and it solves my problem, but i want to get an insight into the design of the component. I do not have the example readily setup to demonstrate my use case. Is there any way to do what i have described? Thank you so much for your time!! |
@sameeri Let me ask you a single question. How would you solve this clearing use case with a regular |
That's a great question @oliviertassinari. I was wondering the same. The whole confusion lies with that principle concept. If we treat the AutoComplete as an input component, then i think we should just not make equality comparison between the old value and the new value. If the component honors inputs, I should not be doing anything special. If i want to set the value of an input, i should just pass it along. For example, the TextField component honors the https://github.com/sameeri/material-ui/blob/master/src/TextField/TextField.jsx This is the same behavior i would expect with Autcomplete(treating it as input) I am gauranteed that the regular |
We can take this discussion to some place better if this is not the right place. I am sorry, it got so long. |
That's a very good point. Using the same logic as the |
Thank you so much @oliviertassinari. Then, the AutoComplete would behave as i expect :) 👍 |
@sameeri Thanks for raising this 👍. Do you want to submit a PR? |
Hey @oliviertassinari I'm so sorry i missed this. Sure, would love to. What's the procedure? |
I am going through the https://github.com/callemall/material-ui/blob/master/CONTRIBUTING.md I will create the PR as soon as i can. Thank you so much for the opportunity. :) 👍 |
@sameeri This was tagged incorrectly, sorry. |
I just had a look at the TextField implementation and that's not the true from what I can see. Sorry, but I can't see better way to do it. |
@oliviertassinari |
@oliviertassinari I've just run into this problem and it's not just an enhancement, it's a bug. The problem is that AutoComplete does not behave like a controlled component. For example:
The problem already started at 2) because inside the AutoComplete component, the values of I only see 2 solutions: I'm willing to do a PR to fix this but I'm not sure which solution to pick, both seem kind of fragile. |
@paol Right, thanks for providing those details. I think that we should be as simple as possible. |
Prerequisites: Scenario
Question |
@nktssh Not I'm aware of. I feel like caching the network answer of the first |
Caching isn't that straightforward, the callee would need to distinguish when to use the cache and when to make a new request, which brings us back to the original problem. Option 1: Add another parameter to the onUpdateInput callback, to make the distinction possible. |
I have another solution in mind. What if we stop mutation the TextField value when the AutoComplete is controlled and we revert #5518? |
Not sure what you mean by stop mutating the TextField, could you explain? |
I think that those lines should be the responsibility of the parent component when controlling the |
Let me see if I understand: you mean remove state.searchText from AutoComplete entirely, and wire props.searchText directly to the TextField value, and presumably wire the TextField onChange to the AutoComplete onUpdateInput. If that can be done then great but it would be a breaking change, no? |
Yeap, that's basically what I'm suggesting. Right, that could be considered a breaking change. |
Well if you make breaking changes may I humbly suggest renaming some props? searchText -> value The first 2 would be a lot more consistent with other controls, and the last would be more intuitive. |
The condition in the componentWillReceiveProps currently compares this.props.searchText and nextProps.
If, on each re-render, we want to have a new value for searchText, like ' ', for example, then we need to compare the nextProps.searchText to this.state.searchText or have no comparison at all & just set it to the passed in value.
Could you please help me understand the behavior for this component. Is this a desired effect? Or is this a bug?
Thank you!
The text was updated successfully, but these errors were encountered: