Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React native refresh component

I have my component that successfully render another component in this way:

const List = require('../List.js');

class MyComponent extends React.Component {
    constructor(props) {
        super(props);
        this.registerListner();
    }
    render() {
        console.log("MyComponent render");
        return (
            <List/>
        );
    }
    registerListener() {
        const emitter = new NativeEventEmitter(MyModule);
        emitter.addListener('onListChange',
            (element) => {
                this.setState({ elements: elements });
                console.log("receive elements: ", elements);
            }
        );
    }
}

module.exports = MyComponent;

Now, how can I render again the list "passing" new elements?

like image 989
Luca Davanzo Avatar asked Apr 28 '17 15:04

Luca Davanzo


1 Answers

When updating the state by setState, react automatically calls render() method again. So all you need to do is:

render() {
    const {elements} = this.state;
    return (
        <List
            items={elements}
        />
    );
}

If the List component has different property name for getting list items, replace items with that name.

like image 168
idealweek.net Avatar answered Nov 18 '22 11:11

idealweek.net