【问题标题】:React Native components not re-render if props changed如果 props 改变,React Native 组件不会重新渲染
【发布时间】:2020-01-31 22:13:38
【问题描述】:

如果 props 发生更改,则不会更新我的组件,但调度的操作是正确的并且store is correct updated。 React 生命周期方法没有监听 props。

只有当状态改变时才会重新渲染组件。 但是 shouldComponentUpdate(),getDerivedStateFromProps()componentDidUpdate() 并没有实现 props 的改变。

如果 props 改变了如何重新渲染组件?

组件:

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;

减速器和动作:

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
    }
}

并存储:

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


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

【问题讨论】:

    标签: javascript reactjs react-native redux react-redux


    【解决方案1】:

    你正在改变你的reducer中的状态,用currentState.todos.push(action.newTodo),它应该是一个纯函数,否则react无法知道props改变了。

    将你的 reducer 函数更新为纯函数

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-26
      • 2020-03-10
      • 1970-01-01
      • 2018-12-12
      • 2019-09-23
      • 1970-01-01
      相关资源
      最近更新 更多