javascript - What are the best practices to get React JS private methods? -


when set component or element callback event, tutorials , documentation show code this:

'use strict'; import react 'react';  let foocomponent = react.createclass({   handleclick(args) {     ...   },   render() {     return <div>       <h1>some title</h1>       <button onclick={this.handleclick}>click me!</button>     </div>   } }; export default foocomponent; 

but handleclick method can accessed out of component, if i'd use foocomponent on component , assign reference can access handleclick other component.

'use strict'; import react 'react'; import foocomponent './foocomponent';  let barcomponent = react.createclass(   handlebarcomponentclick(e) {     this.refs.foocomponent.handleclick(null);   },   render() {     return <div>       <foocomponent ref="foocomponent" />       <button onclick={this.handlebarcomponentclick}>other click</button>     </div>   } ); export default barcomponent; 

i don't fact can access method, in opinion should private or maybe don't have worry it. fix started using "good/bad practice" in projects avoid method being accessed.

'use strict'; import react 'react';  function handleclick(args) {   ... }  let foocomponent = react.createclass({   render() {     return <div>       <h1>some title</h1>       <button onclick={handleclick.bind(this)}>click me!</button>     </div>   } }; export default foocomponent; 

so cannot accessed outside components.

my doubt is, if i'm doing or bad practice, or problems happen or not if continue doing this? or maybe don't have worry set event handlers inside createclass argument?

thanks in advance :)

have checked flux pattern? https://facebook.github.io/react/docs/flux-overview.html

in react apps, not concern. although not define event handlers in private way, general rule never call method on component. if subcomponent needs notify parent of something, accomplished either callback transferred parent child prop or mutating global state (via action) in child component. if, on other hand, parent needs accomplish on child, changes props (or values of such props) on subcomponent.

trying answer question, i'd doing right (defining event handlers in private scope) ok. think more of hassle such thing every handler. suggest review if general architecture of app in line react suggests.


Comments

Popular posts from this blog

how to insert data php javascript mysql with multiple array session 2 -

multithreading - Exception in Application constructor -

windows - CertCreateCertificateContext returns CRYPT_E_ASN1_BADTAG / 8009310b -