记录学习React时自己是如何挖坑把自己埋了的过程:children以及其它props被修改时相关组件无法重新渲染(做了两天)

父组件代码:

class UserHome extends Component<Props, State> implements IUserHome {

  public name: string | undefined;

  public readonly state: State = initialState;

  public handlerClick() {
store.dispatch(sendAction());
} /**
* DOM挂载完成后执行
*/
public componentDidMount() {
store.subscribe(this.subscribe)
} public render() {
return (
<HomeBackground url={BackgroundImg}>
<HomeScreenHeightBox width={1400} background='rgba(246, 248, 249, .92)'>
<button onClick={() => {
this.props.setCount(10)
// console.log(this.state.count)
}}> 点我</button>
<HomeNavigationBar height={this.state.count} />
</HomeScreenHeightBox>
</HomeBackground>
)
}
} const mapDispatchToProps = (dispatch: Function): mapDispatchToPropsInterface => {
return {
sendAction() {
dispatch({
type: 'send_action',
value: "UpYou of blog"
})
},
setCount(sun) {
dispatch({
type: 'set_count',
count: sun
})
}
}
} const mapStateToProps = (state: StateInferface) => {
// console.log(state)
return state;
} export default connect(mapStateToProps, mapDispatchToProps)(UserHome)

readux层

const initialState: StateInferface = {
value: "HELLO WORLD",
count: 50,
height: 50
} const reducer = (state = initialState, action: ActionInterface): any => {
switch (action.type) {
case "send_action":
return Object.assign({ ...state }, action);
case "set_count":
return Object.assign({ ...state }, { count: state.count + 10});
default:
return state;
}
} export default reducer;

现需求是点击“点我”按钮改变HomeNavigationBar组件height属性,HomeNavigationBar代码(这个组件实际上是将height再传给另一个容器组件NavigationBar,一下省略了中间调用代码):

/**
* 首页导航栏容器
*/
export default class HomeNavigationBar extends PureComponent<Props, object> implements IHomeNavigationBar { // 设置默认Props
static defaultProps = {
height: 50,
backColor: "#3a3f51"
} private height?: number = this.props.height; // 导航栏高度
private backColor?: string = this.props.backColor; // 背景颜色
private children?: ReactNode = this.props.children; // 插槽 render() {
const homeNavigationBarStyles = {
height: `${this.height}px`,
backgroundColor: `${this.backColor}`
}
return (
<div id='HomeNavigationBar' style={homeNavigationBarStyles}>
{this.props.height}
<div className="flex-1">
{this.children}
</div>
</div>
);
}
}

NavigationBar中有几段代码导致无法动态改变、重新渲染组件

为了方便直接将props中的值给到height字段,来简单验证一下图中圈起部分代码的可行性:

上图证明无法对基础数据类型的数据进行修改,而是直接将当前变量中的内存地址替换:

let a = 0x213; // 例如等于3
a = 2;// 0x645 改变a的值,实际上改变的是内存地址

所以当props改变时height数据没变化就是这个原因,需要将style中的this.height改为this.props.height,声明的其它变量作废...

由于react中state或props改变时就会触发render方法重新渲染DOM,可以在render中定义height等字段const { children, height, backColor } = this.props;

render() {
const { children, height, backColor } = this.props;
const homeNavigationBarStyles = {
height: `${height}px`,
backgroundColor: `${backColor}`
}
return (
<div id='HomeNavigationBar' style={homeNavigationBarStyles}>
<div className="flex-1">
{children}
</div>
</div>
);
}

React react-redux props或state更新视图无法重新渲染问题的更多相关文章

  1. A Bite Of React(2) Component, Props and State

    component component:用户自己定义的元素 const element = <Welcome name="Sara" />; class Welcome ...

  2. React 深入系列3:Props 和 State

    文:徐超,<React进阶之路>作者 授权发布,转载请注明作者及出处 React 深入系列3:Props 和 State React 深入系列,深入讲解了React中的重点概念.特性和模式 ...

  3. React中Props 和 State用法

    React中Props 和 State用法 1.本质 一句话概括,props 是组件对外的接口,state 是组件对内的接口.组件内可以引用其他组件,组件之间的引用形成了一个树状结构(组件树),如果下 ...

  4. 前端(十一):props、state及redux关系梳理

    所谓状态机,是一种抽象的数据模型,是“事物发展的趋势”,其原理是事件驱动.广泛地讲,世界万物都是状态机. 一.状态机是一种抽象的数据模型 在react中,props和state都可以用来传递数据.这里 ...

  5. react native中对props和state的理解

    最近使用react native这个新的技术做完一个项目,所以赶紧写个博客巩固一下. 今天我想说的是props和state,当然这是我个人的理解,如果有什么不对的地方,望指正. 首先我先说说props ...

  6. react~props和state的介绍与使用

    props是参数的传递,从上层模块向下层模块进行拿传递:而state是提局域变量,一般在本模块内使用,props是不能改变的,而state可以通过setState去修改自身的值. props Reac ...

  7. React中props和state相同点和不同点

    朋友们,我想死你们了,最近这几天忙着和病魔作斗争所以没怎么写博客,今天感觉好点了,赶紧来写一波,就是这木敬业. 今天我们来讨论讨论props和state相同点和不同点 首先我来概要说明一下这两者 pr ...

  8. React Native 快速入门之认识Props和State

    眼下React Native(以后简称RN)越来越火,我也要投入到学习当中.对于一个前端来说,还是有些难度.因为本人觉得这是一个App开发的领域,自然是不同.编写本文的时候,RN的版本为0.21.0. ...

  9. React Native中组件的props和state

    一.组件的属性(props)和状态(state) 1.属性(props) 它是组件的不可变属性(组件自己不可以自己修改props). 组件自身定义了一组props作为对外提供的接口,展示一个组件时只需 ...

随机推荐

  1. VS2015如何调试自己写的DLL与调试

    转载: 1. https://blog.csdn.net/u014738665/article/details/79779632 2. https://blog.csdn.net/jacke121/a ...

  2. Fedora version history --- kernel version

    Fedora version history https://en.wikipedia.org/wiki/Fedora_version_history     Version (Code name)[ ...

  3. git add 添加错文件如何撤销

    git add 添加 多余文件 这样的错误是由于, 有的时候 可能 git add . (空格+ 点) 表示当前目录所有文件,不小心就会提交其他文件 git add 如果添加了错误的文件的话 以下是撤 ...

  4. 0-完全开源的 Vue.js 入门级教程:HelloVue,发车啦!

    本教程基于 Vue.js 2.x 作者:HelloGitHub-追梦人物 欢迎来到 HelloVue-todo-tutorial,这个教程将手把手教你从零开始使用 Vue 搭建一个 Todo(待办事项 ...

  5. BOOST库 消息队列

    直接贴实验代码: /******* boost 消息队列 **********/ #if 1 #include <boost/thread/thread.hpp> #include < ...

  6. spring boot:shardingsphere+druid整合seata分布式事务(spring boot 2.3.3)

    一,shardingshpere为什么要整合seata? 分库分表是数据库扩展中最常用的处理方法, shardingshpere作为使用最广泛的分表中间件, 如果不支持分布式事务,则它的数据一致性就会 ...

  7. JavaSE学习笔记04方法、数组

    1.方法 java方法是语句的集合,它们在一起执行一个功能 方法是解决一类问题的步骤的有序组合 方法包含于类或对象中 方法在程序中被创建,在其他地方被引用 设计方法的原则:一个方法只完成1个功能,这样 ...

  8. CSS常见标签类型

      块级(block): div,ul,li,h1~h6,p 独自占据一行 可设置宽高 行内(inline): span,a 占据所设置字体大小 不可设置宽高 上下margin值不会占用空间 行内块( ...

  9. SLF4J :Failed to load class "org.slf4j.impl.StaticLoggerBinder".

    错误提示 SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder". SLF4J: Defaulting to ...

  10. 2020-2021-1 20209306 《linux内核原理与分析》第一周作业

    学习过程中遇到了如下的问题,通过探索找到了可以解决的方法 1.如何退回主目录或者编辑某个文件夹. 使用cd/home可以退回主目录,这里注意的是绝对路径和相对路径的区别,在学习时我试图切换到home目 ...