Warm tip: This article is reproduced from stackoverflow.com, please click
javascript react-native react-redux reactjs redux

React Native components not re-render if props changed

发布于 2020-04-10 10:06:33

Not updated my component if props changed, but dispatched action is correct and store is correct updated. React lifecycle methods no listened to props.

Only then re-render component if state is changed. But shouldComponentUpdate(),getDerivedStateFromProps() and componentDidUpdate() did not realize props changed.

How be can re-render component if props changed?

Component:

import React, { Component } from 'react';
import { StyleSheet, View, Text, TextInput, TouchableOpacity } from 'react-native';

export default class Main extends Component {
    constructor(props) {
        super(props);
        this.state = {
            todos: []
        };
    }

    addTodo() {
        this.props.addTodo(this.state.title)
    }

    render() {
        return (
            <View>
                <Text style={style.title} >Alma</Text>
                <TextInput style={style.input} placeholder="New todo title" placeholderTextColor="gray" onChangeText={(text) => this.setState({ title: text })} />
                <TouchableOpacity style={{ margin: 20, backgroundColor: "lightblue", padding: 15, borderRadius: 20 }} onPress={() => this.addTodo()} >
                    <View>
                        <Text>Send</Text>
                    </View>
                </TouchableOpacity>

                {
                    this.props.todos.map((e, i) => {
                        return (
                            <Text style={{ textAlign: "center", fontSize: 17, margin: 10 }} key={i} >{e.title}</Text>
                        )
                    })
                }
            </View>
        );
    }
}

const style = StyleSheet.create({
    title: {
        textAlign: "center",
        marginTop: "20%",
        fontSize: 20
    },
    input: {
        borderColor: "black",
        borderWidth: 2,
        borderRadius: 20,
        paddingVertical: 10,
        paddingHorizontal: 20,
        color: "black"
    }
})
import { connect } from 'react-redux'
import { addTodo } from "./reducer";
import Main from "./Main";

function mapStateToProps(state, props) {
    return {
        todos: state.todos
    }
}

function mapDispatchToProps(dispatch, props) {
    return {
        addTodo: (text) => dispatch(addTodo(text))
    }
}

const MainContainer = connect(
    mapStateToProps,
    mapDispatchToProps
)(Main)

export default MainContainer;

reducer and action:

export default function todoReducer(state = { todos: [] }, action) {
    if (action.type === 'ADD_TODO') {
        let currentState = state;
        currentState.todos.push(action.newTodo);

        return currentState;
    }

    return state
}

export function addTodo(title) {
    return {
        type: "ADD_TODO",
        newTodo: title
    }
}

and store:

import { createStore } from 'redux';
import todoReducer from "./reducer";
import { composeWithDevTools } from "redux-devtools-extension";


export default store = createStore(todoReducer,composeWithDevTools());

Questioner
BegGergo
Viewed
17
Charlie 2020-02-01 06:26

You are mutating the state in your reducer, with currentState.todos.push(action.newTodo), it should be a pure function otherwise react has no way to know the props changed.

Update your reducer function to be a pure function

export default function todoReducer(state = { todos: [] }, action) {
  if (action.type === 'ADD_TODO') {
    const todos = [...state.todos, action.newTodo];
    return {...state, todos };
  }

  return state;
}