简体   繁体   中英

Creating a var referencing a store from redux

I am currently working on creating a var that references a store from redux. I created one but within the render() . I want to avoid that and have it called outside of the render. Here is an example of it. I was recommended on using componentWillMount() , but I am not sure how to use it. Here is a snippet of the code I implemented. Note: It works, but only when I render the data. I am using double JSON.parse since they are strings with \\

  render() {

        var busData= store.getState().bus.bus;
        var driverData= store.getState().driver.gdriveras;
        var dataReady = false;

        if (busData&& driverData) {
            dataReady = true;
            console.log("========Parsing bus data waterout========");
            var bus_data_json = JSON.parse(JSON.parse(busData));
            console.log(bus_data_json);
            console.log("========Parsing driver data waterout========");
            var driver_data_json = JSON.parse(JSON.parse(driverData));
            console.log(driver_datat_json);

            busDatajson.forEach(elem => {
                elem.time = getFormattedDate(elem.time)
            });

            driverDatajson.forEach(elem => {
                elem.time = getFormattedDate(elem.time)
            });
            ...
        }
   }

Here is an example of react-redux usage that will probably help you. Don't forget to add StoreProvider to your top three component (often named App ).

I warned you about the fact that React and Redux are not meant to be used by beginner javascript developer. You should consider learn about immutability and functional programming.

// ----

const driverReducer = (state, action) => {

  switch (action.type) {
    // ...
    case 'SET_BUS': // I assume the action type
      return {
        ...state,
        gdriveras: JSON.parse(action.gdriveras) // parse your data here or even better: when you get the response
      }
    // ...
  }

}

// same for busReducer (or where you get the bus HTTP response)
// you can also format your time properties when you get the HTTP response


// In some other file (YourComponent.js)
class YourComponent extends Component {

  render() {
    const {
      bus,
      drivers
    } = this.props

    if (!bus || !drivers) {
      return 'loading...'
    }

    const formatedBus = bus.map(item => ({
      ...item,
      time: getFormattedDate(item.time)
    }))

    const formatedDrivers = drivers.map(item => ({
      ...item,
      time: getFormattedDate(item.time)
    }))

    // return children

  }
}


// this add bus & drivers as props to your component
const mapStateToProps = state => ({
  bus: state.bus.bus,
  drivers: state.driver.gdriveras
}) 

export default connect(mapStateToProps)(YourComponent)
// you have to add StoreProvider from react-redux, otherwise connect will not be aware of your store

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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