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
Post a Comment