2017-01-02 13 views
0

я загружаю .json с Аксиос, и загрузка файлов хорошо, но когда я оказал не работаютне делают JSON с reactjs и Redux

editprofile.js -> создать рассылку, и нагрузка от

JSON
export const editProfile = (callback)=>{ 
     return function(dispatch){ 
      dispatch({type: 'EDIT_PROFILE_REQUEST'}); 
      axios({ 
        method: 'get', 
        url: 'https://gist.githubusercontent.com/anonymous/38c1444f753c70cf79ee980638a14de7/raw/34951eebfa006fea3db00fb492b491ac990c788e/vamos.json', 
        headers: {'Content-Type': 'application/x-www-form-urlencoded'} 
       }) 
      .then((response)=>{ 
       dispatch({type:'EDIT_PROFILE_SUCCESS', payload:response.data}); 
       if (typeof callback === 'function') { 
        callback(null, response.data); 
       } 
      }) 
      .catch((error) =>{ 
       dispatch({type:'EDIT_PROFILE_FAILURE'}); 
       if(error.response.status == 401){ 
        browserHistory.push('login') 
        toastr.error(error.response.message, 'User') 
       } 
       if(typeof callback ==='function'){ 
        callback(error.response.data, null) 
       }   
      }) 
     } 
    } 

EditProfileComponent.jsx -> создал компонент

export default class EditProfileComponent extends Component{ 
     render(){ 
      return(
       <table> 
        <thead> 
         <tr> 
          <th>SN</th> 
          <th>Email</th> 
          <th>created</th> 
         </tr> 
        </thead> 
        <tbody> 
         {this.renderEditProfile()} 
        </tbody> 
       </table> 
      ) 
     }  
     renderEditProfile(){ 
      let sN = 1; 
      return this.props.allProfile.map((user)=>{ 
       return(
        <tr key={user.sN} > 
         <td>{sN++}</td> 
         <td>{user.email ? user.email : '---'}</td> 
         <td>{user.created_at ? user.created_at : '---'}</td> 
        </tr> 
       ); 
      }); 
     } 
    } 

присоединиться к компоненту с сервисом

 import {editProfile} from '../action/editProfile.js'; 
     import EditProfileComponent from '../component/editProfileComponent.jsx'; 

     export default class EditProfileContainer extends Component{ 
      componentDidMount(){ 
       this.props.editProfile(); 
      } 

      render(){ 
       return (
        <EditProfileComponent allProfile={this.props.allProfile} /> 
       ); 
      } 
     } 

     function mapStateToProps(store) { 
      return { 
       allProfile:store.allProfile 
      }; 
     } 

     function matchDispatchToProps(dispatch){ 
      return bindActionCreators({ 
       editProfile:editProfile 
      }, dispatch) 

     } 

     export default connect 

(mapStateToProps, matchDispatchToProps)(EditProfileContainer); 

editProfileReducer -> Редуктор

export const editProfileReducer = (state=[], action) =>{ 
    switch(action.type){ 
     case 'EDIT_PROFILE_REQUEST': 
      return state; 
     case 'EDIT_PROFILE_FAILURE': 
      return state; 
     case 'EDIT_PROFILE_SUCCESS': 
      return [...action.payload]; 
     default: 
      return state; 
    } 
} 

Объединить всех редукторе

import { editProfileReducer } from './reducer/editProfileReducer.js' 

    const reducers = combineReducers({ 
     allProfile:editProfileReducer, 

    }); 

    export default reducers; 

ответ

1

В вашем редукторе есть ошибка. Для EDIT_PROFILE_SUCCESS, он должен быть

case 'EDIT_PROFILE_SUCCESS': 
    return [...state, action.payload]; 

На стороне записки, вы можете воспользоваться функцией стрелки ES6 в:

export const editProfile = (callback) => (dispatch) => { 
    dispatch({type: 'EDIT_PROFILE_REQUEST'}); 
    // .... 
}; 

Вы должны также использовать константы для имен действий.

+0

да что его проблема, спасибо –

0

Я думаю, что есть проблема с:

function mapStateToProps(store) { 
      return { 
       allProfile:store.allProfile 
      }; 
     } 

должно быть:

function mapStateToProps(state) { 
      return { 
       allProfile:state.allProfile 
      }; 
     } 

 Смежные вопросы

  • Нет связанных вопросов^_^