前提・実現したいこと
現在、React+Redux+Laravelで開発しています。
システム的には、質問に対する回答をパラメーターとしてホットペッパーグルメAPIにリクエストし、
そのレスポンスを画面に表示するというものです。
そこで、質問に対する解答を正方形のパネルで用意し、クリックされたところに応じてアクションを発行し、
Reduxのstateを更新したいのですが、うまく更新されません。
エラーも出ていないため、手掛かりもなく…。
どなたか解決方法のご教授お願いいたします。
該当のソースコード
JavaScript
1// containers/Questions/People.js 2 3import React from 'react' 4import { withStyles } from '@material-ui/core/styles' 5import { Link } from 'react-router-dom' 6import { connect } from 'react-redux' 7import { bindActionCreators } from 'redux' 8import * as actions from '../../actions' 9 10const drawerWidth = 250 // Width for Drawer 11 12const styles = (theme) => ({ 13 root: { 14 [theme.breakpoints.up('md')]: { 15 width: `calc(100% - ${drawerWidth}px)`, 16 marginLeft: drawerWidth 17 }, 18 padding: 30, 19 height: '100vh' 20 }, 21 toolbar: theme.mixins.toolbar, // Toolbarの最小の高さ 22 title: { 23 textAlign: 'center', 24 }, 25 boxArea: { 26 display: 'flex', 27 flexWrap: 'wrap', 28 justifyContent: 'center', 29 marginBottom: 40, 30 marginTop: 50 31 }, 32 text: { 33 color: 'white', 34 }, 35 link: { 36 width: '30vmin', 37 height: '30vmin', 38 display: 'flex', 39 justifyContent: 'center', 40 alignItems: 'center', 41 margin: 10, 42 backgroundColor: '#ccc', 43 fontWeight: 'bold', 44 textDecoration: 'none' 45 } 46}) 47 48class People extends React.Component { 49 render() { 50 const { classes, actions } = this.props 51 return ( 52 <div className={classes.root}> 53 <div className={classes.toolbar} /> 54 <h1 className={classes.title}>いまの人数は?</h1> 55 <div className={classes.boxArea}> {/* TODO:クリックしたことがわかるように */} 56 <Link to="budget" className={classes.link}><p className={classes.text} onClick={() => actions.saveAnswers('people', 1)}>1人</p></Link> 57 <Link to="budget" className={classes.link}><p className={classes.text} onClick={() => actions.saveAnswers('people', 2)}>2〜3人</p></Link> 58 <Link to="budget" className={classes.link}><p className={classes.text} onClick={() => actions.saveAnswers('people', 4)}>4〜10人</p></Link> 59 <Link to="budget" className={classes.link}><p className={classes.text} onClick={() => actions.saveAnswers('people', 11)}>11〜20人</p></Link> 60 <Link to="budget" className={classes.link}><p className={classes.text} onClick={() => actions.saveAnswers('people', 21)}>それ以上!</p></Link> 61 </div> 62 </div> 63 ) 64 } 65} 66 67const mapDispatchToProps = dispatch => ({ 68 actions: bindActionCreators(actions, dispatch), 69}) 70 71const StyledPeople = withStyles(styles, { withTheme: true })(People) 72 73export default connect(null, mapDispatchToProps)(StyledPeople)
JavaScript
1// containers/Questions/Budget.js 2 3...大体他と一緒なので略 4 5
JavaScript
1// containers/Questions/Amount.js 2 3...略 4 5class Amount extends React.Component { 6 render() { 7 const { classes } = this.props 8 return ( 9 <div className={classes.root}> 10 <div className={classes.toolbar} /> 11 <h1 className={classes.title}>どれくらい食べたい?</h1> 12 <div className={classes.boxArea}> 13 <Link to="genre" className={classes.link}><p className={classes.text} onClick={() => actions.saveAnswers('amount', 1)}>食べ放題</p></Link> 14 <Link to="genre" className={classes.link}><p className={classes.text} onClick={() => actions.saveAnswers('amount', 2)}>飲み放題</p></Link> 15 <Link to="genre" className={classes.link}><p className={classes.text} onClick={() => actions.saveAnswers('amount', 3)}>両方とも!</p></Link> 16 <Link to="genre" className={classes.link}><p className={classes.text} onClick={() => actions.saveAnswers('amount', 4)}>どっちでも</p></Link> 17 </div> 18 </div> 19 ) 20 } 21} 22...略
JavaScript
1// containers/Questions/Genre.js 2 3...略 4 5class Genre extends React.Component { 6 constructor(props) { 7 super(props) 8 this.handleSubmit = this.handleSubmit.bind(this) 9 } 10 11 handleSubmit() { 12 /** 13 * ここでconsoleに出力して確認している 14 */ 15 console.log(this.props.QuestionsReducer.genre) 16 console.log(this.props.QuestionsReducer.people) 17 console.log(this.props.QuestionsReducer.freeDrink) 18 console.log(this.props.QuestionsReducer.freeFood) 19 console.log(this.props.QuestionsReducer.budget) 20 } 21 22 render() { 23 const { classes, actions } = this.props 24 return ( 25 <div className={classes.root}> 26 <div className={classes.toolbar} /> 27 <h1 className={classes.title}>何が食べたい?</h1> 28 <div className={classes.boxArea}> 29 <Link to="genre" className={classes.link}><p className={classes.text} onClick={() => actions.saveAnswers('genre', '居酒屋')}>居酒屋</p></Link> 30 <Link to="genre" className={classes.link}><p className={classes.text} onClick={() => actions.saveAnswers('genre', '和食')}>和食</p></Link> 31 <Link to="genre" className={classes.link}><p className={classes.text} onClick={() => actions.saveAnswers('genre', 'ラーメン')}>ラーメン</p></Link> 32 <Link to="genre" className={classes.link}><p className={classes.text} onClick={() => actions.saveAnswers('genre', '中華')}>中華</p></Link> 33 <Link to="genre" className={classes.link}><p className={classes.text} onClick={() => actions.saveAnswers('genre', '焼肉')}>焼肉</p></Link> 34 <Link to="genre" className={classes.link}><p className={classes.text} onClick={() => actions.saveAnswers('genre', '韓国料理')}>韓国料理</p></Link> 35 <Link to="genre" className={classes.link}><p className={classes.text} onClick={() => actions.saveAnswers('genre', 'カフェ・スイーツ')}>カフェ<br />・<br />スイーツ</p></Link> 36 <Link to="genre" className={classes.link}><p className={classes.text} onClick={() => actions.saveAnswers('genre', 'それ以外')}>それ以外!</p></Link> 37 </div> 38 <p><a className={classes.start} onClick={this.handleSubmit}>検索する!</a></p> 39 <Link to="/" className={classes.backLink}><p className={classes.back}>やり直す</p></Link> 40 41 </div> 42 ) 43 } 44} 45...略
JavaScript
1// actions/index.js 2 3...略 4 5// Save answer 6export const saveAnswers = (questionType, answer) => { 7 switch (questionType) { 8 case 'people': 9 return { 10 type: actionTypes.PEOPLE_ANSWER, 11 answer: answer 12 } 13 case 'budget': 14 return { 15 type: actionTypes.BUDGET_ANSWER, 16 answer: answer 17 } 18 case 'amount': 19 return { 20 type: actionTypes.AMOUNT_ANSWER, 21 answer: answer 22 } 23 case 'genre': 24 return { 25 type: actionTypes.GENRE_ANSWER, 26 answer: answer 27 } 28 default: 29 return 30 } 31}
JavaScript
1// reducers/QuestionsReducer.js 2 3import * as actionTypes from '../utils/actionTypes' 4 5const initialState = { 6 people: 0, 7 budget: '', 8 freeFood: 0, 9 freeDrink: 0, 10 genre: '', 11} 12 13const QuestionsReducer = (state = initialState, action) => { 14 switch (action.type) { 15 case actionTypes.PEOPLE_ANSWER: 16 return { 17 ...state, 18 people: action.answer 19 } 20 case actionTypes.BUDGET_ANSWER: 21 return { 22 ...state, 23 budget: action.answer 24 } 25 case actionTypes.AMOUNT_ANSWER: 26 switch (action.answer) { 27 case 1: 28 return { 29 ...state, 30 freeFood: 1 31 } 32 case 2: 33 return { 34 ...state, 35 freeDrink: 1 36 } 37 case 3: 38 return { 39 ...state, 40 freeFood: 1, 41 freeDrink: 1 42 } 43 default: 44 return state 45 } 46 case actionTypes.GENRE_ANSWER: 47 return { 48 ...state, 49 genre: action.answer 50 } 51 default: 52 return state 53 } 54} 55 56export default QuestionsReducer
補足情報(FW/ツールのバージョンなど)
Laravel 5.8.*
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。