2017-11-21 1 views
0

다른 클래스의 상태를 변경REACTJS : 나는이 두 파일 Reactjs <br> file1.js이

class App extends Component { 
constructor(props) { 
    super(props); 
    this.state = { 
     data: [], 
     selection: null 
    }; 

file2.js

class SearchBar extends Component { 
InputChange =() => { 
    console.log(App.state); 
}} 

내가 사용하고 클래스를 사용하면 클래스 응용 프로그램의 상태를 변경하려면 SearchBar, file2를 file1로 가져 왔습니다. 고맙습니다! 내 영어가 좋지 않아. 이건 정말 코드를 축약 된

class SearchBar extends Component { 
InputChange = (params) => { 
    this.props.changeHandler(params); 
}} 

당신의 SearchBar에 구성 요소에 다음

class App extends Component { 
    constructor(props) { 
    super(props); 
    this.state = { 
     data: [], 
     selection: null 
    }; 

    changeStuff(paramsIfAny) { 
    this.setState(/* whatever you want */); 
    } 

    render() { 
    ..... 
    <SearchBar changeHandler={this.changeStuff.bind(this)} /> 
    ..... 
    } 
} 

그리고 : D 방금 상태과 같이 변경 기능에 전달해야

+0

do file1과 file2는 anyShip, –

+2

을 공유합니다. React 가이드를 읽는 것이 좋습니다. https://reactjs.org/docs/lifting-state-up.html – Kunukn

답변

3

이 용이하다 내가 의미했던 것을 단순화시킨다. 어떻게 완료했는지 알았 으면 좋겠어!