reactjs - Stop removal of React app items from deleting Firebase state object -
i developing app learn react, whereby users can log monthly income, bills , transactions. state has object called cashbook 3 nested objects (income: {}, expenditure: {}, transactions: {})
getinitialstate() { return { cashbook: { expenditure: {}, income: {}, transactions: {} } } } i had working nicely, based on each object being defined in firebase. trying make work app still functions firebase not having of objects defined. (i describing terribly, aren't i?). so, user has data in each object within state, , removes data expenditure, app errors because trying output data expenditure state object, removing items removes object in firebase, , app state. (i using tyler mcginnis' rebase (https://github.com/tylermcginnis/re-base)
the following app's render, componentdidmount , expenditure's render methods, might explain better.
(the error relates line in expenditure react class: {object.keys(this.props.cashbook).map(this.renderexpenditure)} , error reads:
uncaught typeerror: cannot convert undefined or null object
const app = react.createclass({ getinitialstate() { return { cashbook: { expenditure: {}, income: {}, transactions: {} } } }, componentdidmount() { base.syncstate('cashbook', { context: this, state: 'cashbook' }); }, removecashflow(key, type) { this.state.cashbook[type][key] = null; this.setstate({ cashbook: { [type]: this.state.cashbook[type] } }); }, render() { return( <div classname="cashbook"> <a classname="button" onclick={this.loadsampledata}>load sample data</a> <expenditure cashbook={this.state.cashbook.expenditure} removecashflow={this.removecashflow} /> </div> ); } }); and expenditure:
const expenditure = react.createclass({ renderexpenditure(key) { const details = this.props.cashbook[key]; return( <tr classname="item" key={key}> <td><strong>{details.name}</strong></td> <td><strong>{h.formatprice(details.amount)}</strong></td> <td>{details.category}</td> <td>{details.type}</td> <td>{details.date}</td> <td><button classname="remove-item" onclick={this.props.removecashflow.bind(null, key, 'expenditure')}>remove</button></td> </tr> ); }, render() { return( <div classname="expenditure"> <table id="exp-table"> <tbody> {object.keys(this.props.cashbook).map(this.renderexpenditure)} </tbody> </table> </div> ); } });
sorted - ended using:
{object.keys(this.props.cashbook || {}).map(this.renderexpenditure)} this ensured if there nothing in cashbook object, empty object used.
Comments
Post a Comment