LoginSignup
0
0

More than 3 years have passed since last update.

reactでstateを別コンポーネントに渡すときの呼び出される側

Posted at

呼ばれる側

  • コンポーネントが呼ばれて返す値にstateをセットすると別コンポーネントから呼び出すことができる
class GetPod extends Component {
    constructor(props) {
        super(props);
        this.state = {
            podstatus: ''
        }
    }

    // renderの前に実行される
    componentWillMount() {
    const request = axios.create({
            baseURL: "http://127.0.0.1:1323",
            method: "GET"
        })
        request.get("/getpodstatus")
            .then(request => {
                this.setState({
                    podstatus: request.data.items[0].status.phase
                })
                console.log(this.state.podstatus)
            })
    }

    // コンポーネントが呼ばれたらstateを返すようにする
    render() {
        return <p>{this.state.podstatus}</p> ;
    }
}
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0