表題の通りです。
以下がコードになります。
js
1Task.js 2 3import React from "react" 4import PropTypes from "prop-types" 5class Task extends React.Component { 6 constructor(props){ 7 super(props) 8 this.state = { 9 name: '', 10 desctiprtion: '' 11 } 12 13 } 14 15 render () { 16 const list = this.props.tasks.map((task, i) => <li key={i}>{task}</li>) 17 return ( 18 <div> 19 <ul>{list}</ul> 20 <input type="text" onChange={e => this.setState({name: e.target.value})}></input> 21 <input type="text" onChange={e => this.setState({descriptiton: e.target.value})}></input> 22 <button onClick={() => this.props.addTask(this.state)}>Post</button> 23 </div> 24 ); 25 } 26} 27 28export default Task
js
1taskContainer.js 2 3import { connect } from 'react-redux'; 4import * as actions from '../actions/taskAction'; 5import Task from '../components/Task'; 6 7const mapStateToProps = state => ( 8 { 9 tasks: state.tasks 10 } 11) 12 13const mapDispatchToProps = dispatch => { 14 return { 15 addTask: (task) => dispatch(actions.addTask(task)) 16 } 17} 18 19const ConnectedTask = connect(mapStateToProps, mapDispatchToProps)(Task) 20export default ConnectedTask
js
1taskAction.js 2 3export const addTask = (task) => { 4 return { 5 type: 'ADD_TASK', 6 payload: { task: task } 7 } 8}
js
1taskReducer.js 2 3const initialState = { 4 tasks: [] 5} 6 7export const taskReducer = (state = initialState, action) => { 8 switch(action.type){ 9 case 'ADD_TASK': 10 const task = action.payload.task 11 const newState = Object.assign({}, state) 12 newState.tasks.push(task) 13 return newState; 14 default: 15 return state 16 } 17}
js
1configureStore.js 2 3import { createStore } from 'redux' 4import { taskReducer } from './reducers/taskReducer' 5 6const configureStore = () => { 7 const store = createStore(taskReducer) 8 console.log(store) 9 return store 10} 11 12export default configureStore
js
1App.js 2 3import React from 'react' 4import { Provider } from 'react-redux' 5import ConnectedTask from '../containers/taskContainer' 6import configureStore from '../configureStore' 7 8const store = configureStore(); 9 10class App extends React.Component { 11 render() { 12 return ( 13 <Provider store={store}> 14 <ConnectedTask /> 15 </Provider> 16 ) 17 } 18} 19 20export default App 21
一番上のTask.jsがコンポーネント でformに値を入れてaddTaskを発火させても、Task.jsがrerenderされず、
postした値が描画されません。。。
何卒よろしくお願いします。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2020/06/12 06:29