简体   繁体   English

如何使用ReactJs显示,添加和删除JSON文件中的项目

[英]How to display, add and delete items from JSON file using ReactJs

I am using ReactJs and I am trying to create CRUD app that will be able to delete, display and add item to JSON file. 我正在使用ReactJs,并且尝试创建CRUD应用程序,该应用程序将能够删除,显示项目并将其添加到JSON文件。 With ReactJs I am also using Redux. 使用ReactJs我也使用Redux。 I am trying to learn how to use ReactJs and Redux together. 我正在尝试学习如何一起使用ReactJs和Redux。

JSON file will act like server. JSON文件将充当服务器。

This is my structure for now: 这是我现在的结构:

在此处输入图片说明

index.js index.js

const logger = createLogger();
const store = createStore(combineReducers({roomReducer}), {}, applyMiddleware(logger));


ReactDOM.render(

  <Provider store = {store}>
  <BrowserRouter>
    <App/>
  </BrowserRouter>
  </Provider>,

  document.querySelector("#root")
);

App.js App.js

export default class App extends React.Component {
    render() {
        return (
            <div>
            <Route path="/" exact component={LandingPage} /> 

          </div>
        )
    }
}

REDUCERS 减速器

index.js index.js

import {combineReducers} from 'redux';
import rooms from '../templates/rooms/Rooms';

const roomsReducer = combineReducers({
  rooms
});

export default roomsReducer;

roomReducer.js roomReducer.js

const initialState = {
    rooms: []
  };

  export default function cardReducer(state = initialState, action) {
    switch (action.type){
      case 'ADD_ROOM':
        return {
          ...state,
          rooms: [
            ...state.rooms,
            action.rooms
          ]
        }
      case 'REMOVE_ROOM':

        //not sure how to do it

      case 'FETCH_ROOMS':
        return Object.assign({}, state, {
          cards: action.rooms
        });
      default:
        return state;
    }
  }

ACTIONS 动作

roomActions.js roomActions.js

 export function fetchRooms(){
    return {
      type: 'FETCH_ROOMS'
    }
  }

  export function addRoom(){
    return {
      type: 'ADD_ROOM'
    }
  }

  export function removeRoom(id){
    return {
      type: 'REMOVE_ROOM',
      id: id
    }
  }

db.json db.json

 {
 "cards": [
    {
      "id": 0,
      "image": "http://placehold.it/400x300",
      "title": "CATEGORY 0",
      "numberGuests": "Placeholder 0",
      "type": "Single"
    },
    {
      "image": "http://placehold.it/400x300",
      "title": "CATEGORY AAAA",
      "numberGuests": "Placeholder BBB",
      "type": "Single",
      "id": 6
    },
    {
      "image": "http://placehold.it/400x300",
      "title": "CATEGORY AAAA",
      "numberGuests": "Placeholder BBB",
      "type": "Single",
      "id": 7
    }
  ]
}

This code is what I have came up by now. 这段代码是我现在提出的。 I have created store in my index.js file and connected it to my rooms.js component 我已在index.js文件中创建商店,并将其连接到rooms.js组件

class Rooms extends React.Component{

    render(){
        return(
            <div>

                hello

            </div>
        );
    }
}

const mapStateToProps = (state) => {
    return{
        room: state.roomReducer
    };
};


export default connect(mapStateToProps)(Rooms);

I hope I did it correctly. 我希望我做得正确。 I am not sure how to now add, delete or list rooms that I have already in db.json file. 我不确定现在如何添加,删除或列出db.json文件中已有的db.json

Any advice or guideline is highly appreciated. 任何建议或准则都将受到高度赞赏。

 let dbjson = { "cards": [ { "id": 0, "image": "http://placehold.it/400x300", "title": "CATEGORY 0", "numberGuests": "Placeholder 0", "type": "Single" }, { "image": "http://placehold.it/400x300", "title": "CATEGORY AAAA", "numberGuests": "Placeholder BBB", "type": "Single", "id": 6 }, { "image": "http://placehold.it/400x300", "title": "CATEGORY AAAA", "numberGuests": "Placeholder BBB", "type": "Single", "id": 7 } ] }; let state = {}; //You have cards in state state['rooms'] = dbjson.cards; // add room let roomItem = { "image": "http://placehold.it/400x300", "title": "CATEGORY AAAA", "numberGuests": "Placeholder BBB", "type": "Single", "id": 8 }; let rooms = [...state.rooms,roomItem]; state['rooms'] = rooms; //dispatch // remove room by index let index = 1; let roomstodelete = [...state.rooms]; roomstodelete.splice(1,index); state['rooms'] = roomstodelete; //dispatch // remove room by id let roomstodeleteByid = [...state.rooms].filter(vl=>vl.id!==8); state['rooms'] = roomstodeleteByid; //dispatch 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM