reactjs - React JS - how use/change states or function with imported Components -


i'm new in reactjs , big question is... idea divide solution in other files organize code? think better lot of components in same file, but, divide in files brings 1 big problem, how use/change states or functions between files?

file index.jsx

// index.jsx import secondcomponent '/components/secondcomponent.jsx'  class firtstcomponent extends react.component {     state = {         mystate: false     } } 

file secondcomponent.jsx

// secondcomponent.jsx class secondcomponent extends react.component {     setstate = ({         this.setstate({ mystate: true })     }) } 

how can change state of firstcomponent secondcomponent?

i'm not 100% sure asking, think want this:

*firstcomponent.jsx* class firtstcomponent extends react.component {     state = {         mystate: false     }      const myfunction = () => {         this.setstate({ mystate: true })     }      render() {       return (         <secondcomponent function={myfunction} />       )     } }  *secondcomponent.jsx* export default class secondcomponent extends react.component {     render() {       return (         <div onclick={this.props.function} />       )     } } 

Comments

Popular posts from this blog

php - Vagrant up error - Uncaught Reflection Exception: Class DOMDocument does not exist -

vue.js - Create hooks for automated testing -

Add new key value to json node in java -