質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
React.js

Reactは、アプリケーションのインターフェースを構築するためのオープンソースJavaScriptライブラリです。

Q&A

解決済

1回答

422閲覧

子のcomponentから親のstateを更新させた後に子が持ってるpropsを更新させたい

退会済みユーザー

退会済みユーザー

総合スコア0

React.js

Reactは、アプリケーションのインターフェースを構築するためのオープンソースJavaScriptライブラリです。

0グッド

0クリップ

投稿2018/03/13 08:45

編集2018/03/14 10:03

あるcomponentでcomponentWillUpdate()をし、
state、またはpropsが更新されたタイミングで子のcomponentにpropsで親のcomponentのstateを渡したいのですがどうすればできますか?

コード

親component

import React from 'react'; import PropTypes from 'prop-types'; import component1 from './component1'; import component2 from './component2'; class parent extends React.Component { constructor(props) { super(props); this.state = { array: [ { test: 'test1', test1: false, index: 0}, { test: 'test2', test1: false, index: 1}, { test: 'test3', test1: false, index: 2}, ], }; } onSubmitState() { this.setState({ array: [ { test: 'aaa', test1: true, index: 0}, { test: 'bbb', test1: true, index: 1}, { test: 'ccc', test1: true, index: 2}, ], }); } cmp1Props(obj) { return ( <component1 cmp1Props={obj} /> ); } cmp2Props(obj) { return ( <component2 cmp2Props={obj}, onSubmitState={() => {this.onSubmitState();}} /> ); } render() { const cmp1 = this.state.array.map((e) => { return ( this.cmp1Props(e) ); }); const cmp2 = this.state.array.map((e) => { return ( this.cmp2Props(e) ); }); return ( <div> {cmp1} {cmp2} </div> ); } } export default parent;

component1(子)

import React from 'react'; import PropTypes from 'prop-types'; class component1 extends React.Component { static propTypes = { cmp1Props: PropTypes.object, }; constructor(props) { super(props); this.state = { test: this.props.cmp1Props.test, test1: this.props.cmp1Props.test1, }; } render() { if(this.state.test1) { return( <div> <p>成功</p> </div> ); } else { return( <div> <p>失敗</p> </div> ); } } } export default component1;

component2(子)

import React from 'react'; import PropTypes from 'prop-types'; const propTypes = { cmp2Props: PropTypes.object, onSubmitState: PropTypes.func }; class component2 extends React.Component { constructor(props) { super(props); this.state = { index: this.props.cmp2Props.index, }; } buttonClick() { return this.props.onSubmitState(); } render() { return( <div> <button onClick={() => {this.buttonClick();}}>ボタン</button> </div> ); } } component2.propTypes = propTypes; export default component2;

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

親のコンポーネントの state を、子のコンポーネントで変更するには、親のコンポーネントが自身の state を変更するメソッドを子に渡すことで実現できます。

文章で書くと分かりづらいですが、コードを見ると単純です。
https://codepen.io/og24715/pen/QrJEEP?editors=0010

Container(親) は state を持ち、 Countコンポーネントを描画します。
Count(子) コンポーネントは props を元にカウントと、カウントアップするボタンを表示します。

js

1const Counter = ({ handleClick, counter }) => ( 2 <div> 3 <p>{counter}</p> 4 <button onClick={handleClick}>Add</button> 5 </div> 6); 7 8class Container extends React.Component { 9 state = { counter: 0 }; 10 11 render() { 12 return ( 13 <Counter 14 counter={this.state.counter} 15 handleClick={this._addCounter} 16 /> 17 ); 18 }; 19 20 _addCounter = () => { 21 const counter = this.state.counter + 1; 22 this.setState({ counter }); 23 }; 24} 25 26ReactDOM.render( 27 <Container />, 28 document.getElementById('root'), 29); 30

投稿2018/05/17 14:32

og24715

総合スコア832

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問