madvas / cljs-react-material-ui

Clojurescript library for using material-ui.com
Eclipse Public License 1.0
205 stars 32 forks source link

Acess component state #11

Closed alberto-portilla closed 8 years ago

alberto-portilla commented 8 years ago

How can i access the state of a component? For instance, i need to show the error message only if the content doesn't comply with a schema and its state is not clean.

madvas commented 8 years ago

Can you be more specific? with Om you access state like (om/get-state this). Please provide some code example if possible.

alberto-portilla commented 8 years ago

Sure! Every material-ui component has a state key isClean (along with some others as hasValueor isFocused. You can check this with react tools by inspecting a TextField component. usually you can access this state key in jsx by using {this.state.isClean}. I cant figure out how to do this while declaring the component with the library, i could use component refs, but this breaks encapsulation.

The (om/get state this) needs a component, and im not able to get the reference to react object in the cljs context, because (this-as)return a plain js object. Id appreciate so much if you can help me

madvas commented 8 years ago

Okay, I see. I don't think you can (and should) access it's internal state from the outside. What should be accessed from outside is in their docs http://www.material-ui.com/#/components/text-field . For your particular problem, error text in material-ui works like, if errorText property is not empty error is displayed otherwise not. If this isn't sufficient for your problem, you can still make wrapper component, which keeps its internal state however you want and creates text-field accordingly.

alberto-portilla commented 8 years ago

Understood, ill follow your advice on creating a wrapper component. Thanks for the tip!