react - 为什么在Child上使用更新状态时父组件状态会发生变化?

时间:2016-11-23 03:36:01

标签: javascript reactjs brunch

当我通过这样的方式修改子组件的状态时:

// inside child component
var stateToSet = this.state;
stateToSet[active] = false;
this.setState(nextState);

父状态在没有重新渲染的情况下发生变化。

我想在this.state时引用了父组件stateToSet[active] = false,所以我Object.assign了解了我要修改的状态和每个属性,然后它没有工作修改父母的状态。

我想现在为什么会这样。这可能是预期的行为吗?早午餐(我的编译器)和连接文件可能是问题吗?

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

是的,所以你永远不应该像你在这里一样直接修改状态,因为 # Sets the minimum version of CMake required to build the native # library. You should either keep the default value or only pass a # value of 3.4.0 or lower. #cmake_minimum_required(VERSION 3.4.1) # Creates and names a library, sets it as either STATIC # or SHARED, and provides the relative paths to its source code. # You can define multiple libraries, and CMake builds it for you. # Gradle automatically packages shared libraries with your APK. add_library( # Sets the name of the library. native-lib # Sets the library as a shared library. SHARED # Provides a relative path to your source file(s). # Associated headers in the same location as their source # file are automatically included. src/main/cpp/native-lib.cpp ) # Searches for a specified prebuilt library and stores the path as a # variable. Because system libraries are included in the search path by # default, you only need to specify the name of the public NDK library # you want to add. CMake verifies that the library exists before # completing its build. find_library( # Sets the name of the path variable. log-lib # Specifies the name of the NDK library that # you want CMake to locate. log ) # Specifies libraries CMake should link to your target library. You # can link multiple libraries, such as libraries you define in the # build script, prebuilt third-party libraries, or system libraries. target_link_libraries( # Specifies the target library. native-lib # Links the target library to the log library # included in the NDK. ${log-lib} ) 实际上会触发组件的所有生命周期方法,而像这里修改状态则不会。该功能会使用您要修改的密钥,因此无需this.setStateObject.assign

...

我不确定您对父母/孩子的意思,以及为什么设置孩子的父母与父母有什么关系,但如果你的意思是当孩子的状态发生变化时父母没有被重新呈现,那么这是预期的行为。