###実現したいこと
App.jsにてdidMountした値をContentに渡し、Content.jsにて受け取り、テキストボックスに表示されるようにしたい
###実装の結果・問題点
Content.jsにどうのように受け取り、表示させたら良いかわからず。
とりあえずコードを書いてみたのですが、想定していた値が表示されず。
Reduxの概念とうまく結びつけられていないのでしょうか?
実現するためのヒントをいただけたら幸いです。よろしくお願いします。
▼実装結果(idとnameのところにdidMountした値が表示されるようにしたいがされなかった)
▼ App.js
JavaScript
1import React, { Component } from 'react'; 2 3import Aside from './components/Aside'; 4import Header from './components/Header'; 5import Footer from './components/Footer'; 6import Top from './components/Top'; 7import Content from './components/Content'; 8import List from './components/List'; 9 10 11class App extends Component { 12 constructor(props) { 13 super(props); 14 this.state = { 15 id: [], 16 name: [], 17 }; 18 } 19 componentDidMount() { // 追加 20 const id = 4 21 const name = "shiro" 22 this.props.didMnt(id,name); 23 } 24 addReg(id,name) { 25 this.setState({ 26 id: [this.state.id,id], 27 name: [this.state.name,name] 28 }); 29 } 30 formId 31 onChangeId = (e) => { 32 console.log(e.target.value) 33 this.props.updateFormId(e.target.value); 34 } 35 // forName 36 onChangeName = (e) => { 37 console.log(e.target.value) 38 this.props.updateFormName(e.target.value); 39 } 40 // clickSaveButton 41 onSaveItem = () => { 42 console.log(this.props) 43 console.log("SAVE_ID: ",this.props.id); 44 console.log("SAVE_NAME: ",this.props.name); 45 } 46 47 render() { 48 // const formItem = this.props 49 const id = this.props.id; 50 const name = this.props.name; 51 52 //NAME3文字以上ででsaveボタン押せるようにdisabledを入れる 53 const checkName = this.props.name.length >=3; 54 55 return ( 56 <> 57 <Aside /> 58 <Header /> 59 sample site 60 <Top /> 61 <div id="register"> 62 <List/> 63 <Content 64 onChangeId={this.onChangeId} 65 onChangeName={this.onChangeName} 66 onSaveItem={this.onSaveItem} 67 checkName={checkName} 68 onAddReg={(id,name)=>this.addReg(id,name)} 69 /> 70 </div> 71 <Footer /> 72 </> 73 ); 74 } 75} 76export default App;
▼Content.js
JavaScript
1'use strict'; 2import React,{ useState } from 'react'; 3import { createStore } from "redux"; 4import { id, name } from '../reducers'; 5 6export default function Content(props) { 7 console.log(props) 8 return ( 9 <> 10 <p>content</p> 11 <Form 12 // id={props.id} 13 // name={props.name} 14 onChangeId={props.onChangeId} 15 onChangeName={props.onChangeName} 16 onAddReg={(id,name)=>this.addReg(id,name)} 17 /> 18 <button type="button" 19 className="btn btn-outline-success" 20 onClick={(e) => {props.onSaveItem();console.log(e)}} 21 disabled = {!props.checkName} 22 > 23 SAVE 24 </button> 25 </> 26 ); 27} 28 29function Form(props) { 30 return ( 31 <form> 32 <div className="form-group"> 33 <label className="pt-2">ID</label> 34 <input id="id" 35 type="text" 36 className="form-control" 37 value={props.id} 38 onChange={props.onChangeId} 39 /> 40 <label className="pt-2">Name</label> 41 <input id="name" 42 type="text" 43 className="form-control" 44 value={props.name} 45 onChange={props.onChangeName} 46 /> 47 </div> 48 </form> 49 ) 50}
▼ conteners/App.js
JavaScript
1import { connect } from 'react-redux'; 2import * as actions from '../actions'; 3import App from '../App'; 4 5const mapStateToProps = state => { 6 const {id,name} = state; 7 return {id,name} 8} 9 10const mapDispatchToProps = dispatch => { 11 return { 12 updateFormId: (id) => dispatch(actions.updateFormId(id)), 13 updateFormName: (name) => dispatch(actions.updateFormName(name)), 14 didMnt: (id, name) => dispatch(actions.didMnt(id, name)), 15 } 16} 17export default connect(mapStateToProps, mapDispatchToProps)(App)
▼actions.js
JavaScript
1export const didMnt = (id, name) => { 2 return { 3 type: 'DID_MOUNT', 4 payload: { id, name } 5 }; 6} 7 8export const updateFormId = (id) => { 9 return { 10 type: 'UPDATE_ID', 11 payload: { id } 12 }; 13} 14export const updateFormName = (name) => { 15 return { 16 type: 'UPDATE_NAME', 17 payload: { name } 18 }; 19}
▼reducer.js
JavaScript
1'use strict'; 2 3const initialStateId = "" 4export function id(state = initialStateId, action) { 5 console.log(action); 6 let newState = JSON.parse(JSON.stringify(state)); 7 switch (action.type) { 8 case 'UPDATE_ID': 9 newState = action.payload.id; 10 break; 11 default: 12 return state; 13 } 14 console.log(newState) 15 return newState; 16}; 17 18const initialStateName = "" 19export function name(state = initialStateName, action) { 20 console.log(action); 21 let newState = JSON.parse(JSON.stringify(state)); 22 switch (action.type) { 23 case 'UPDATE_NAME': 24 newState = action.payload.name; 25 break; 26 default: 27 return state; 28 } 29 return newState; 30};
▼createStore.js
JavaScript
1import { createStore as reduxCreateStore, combineReducers } from "redux"; 2import { id, name } from "./reducers"; 3 4export default function createStore() { 5 const store = reduxCreateStore( 6 combineReducers({ 7 id, 8 name 9 }) 10 ); 11 return store; 12}
あなたの回答
tips
プレビュー