【问题标题】:React Component does not render after the state changes状态改变后 React Component 不渲染
【发布时间】:2020-07-21 15:34:20
【问题描述】:

您好,我在重新渲染下面的组件时遇到问题,您可以在第一个屏幕中看到我的代码,其中我有一个自定义组件:

class Ahelle extends React.Component{
    constructor(props){
        super(props)
        this.state={
            mamad:10
        }
    }
    render(){
        return (
            <View style={{backgroundColor:"white"}}>
                <ScrollView>
              <CustomSlider  defaultValue={0} />
            <CustomSlider  defaultValue={this.state.mamad} disabled={true}/>
           
           <TouchableOpacity style={{width:100,height:100,alignSelf:"center"}} onPress={()=>{this.setState({mamad:20})}}><Text>dddd</Text></TouchableOpacity>
              </ScrollView>
            </View>
          
        );
    }
 
}

这里我有一个自定义组件,我传递了一个默认值来显示,但是当我改变 state 时,它​​并没有改变我作为 props 传递的值。 这是我的自定义滑块组件及其状态、道具和任何其他详细信息。

class Test extends React.Component{
    constructor(props){
        console.log(props)
        super(props)
        this.state = {
            value: props.defaultValue
          };
       
    }
    render(){
        return(

                   <Slider
    style={{width: wp("70%")}}
    value={this.state.value}
  />
                 
         )
    }
}
export default Test;

SEE THE PROBLEM IN ACTION 感谢您的宝贵时间

【问题讨论】:

  • 代码似乎没问题。也许您正在检查第一个 CustomSlider 中的道具。您有 2 个自定义滑块。一个是defaultValue={0},另一个是defaultValue={this.state.mamad}
  • 这似乎应该引发关于将不受控组件更改为受控组件的错误。默认值不应该改变。您的意思是传递 this.state.mamad 作为值吗? 为了将来参考,请发布代码 sn-ps 与图像,它们更难阅读且不可搜索。
  • 不,我以两个为例,当我设置新状态时,传递的值不会改变@kapilpandey
  • 是的,我想在此页面上进行一些计算后更改值@DrewReese
  • @DrewReese 我编辑并发布了代码,

标签: javascript reactjs react-native state


【解决方案1】:

您的滑块组件永远不会对传递的更新的道具值做任何事情。巧合的是,将传递的道具存储在本地组件状态中也是一种反应反模式。您可以而且应该直接食用它们。只需将 this.state.mamad 传递给 value 道具并使用即可。您还可以通过将任何其他道具分散到滑块组件中来传递它们。

class Test extends React.Component {
  render() {
    return (
      <Slider
        style={{ width: wp("70%") }}
        value={this.props.value}
        {...this.props}
      />
    );
  }
}

export default Test;

用法

<CustomSlider value={this.state.mamad} disabled={true} />

如果你真的想要存储传递的defaultValue 属性并保持更新,那么实现componentDidUpdate 生命周期函数。请注意,这不是推荐的解决方案。

class Test extends React.Component {
  constructor(props) {
    console.log(props);
    super(props);
    this.state = {
      value: props.defaultValue
    };
  }

  componentDidUpdate(prevProps) {
    const { defaultValue } = this.props;
    if (prevProps.defaultValue !== defaultValue) {
      this.setState({ value: defaultValue});
    }
  }

  render() {
    return <Slider style={{ width: wp("70%") }} value={this.state.value} />;
  }
}

export default Test;

【讨论】:

  • 非常感谢,是的,我没注意保存状态,
【解决方案2】:

您的代码是正确的。我测试过了。

App.js

import React from "react";
import { Text, View, ScrollView, TouchableOpacity } from "react-native";
import CustomSlider from "./CustomSlider";

export default class Ahelle extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      mamad: 10,
    };
  }
  render() {
    return (
      <View style={{ backgroundColor: "white" }}>
        <ScrollView>
          <CustomSlider defaultValue={0} />
          <CustomSlider defaultValue={this.state.mamad} disabled={true} />

          <TouchableOpacity
            style={{ width: 100, height: 100, alignSelf: "center" }}
            onPress={() => {
              this.setState({ mamad: 20 });
            }}
          >
            <Text>dddd</Text>
          </TouchableOpacity>
        </ScrollView>
      </View>
    );
  }
}

CustomSlider.js

import React from "react";
import { Text } from "react-native";

export default class CustomSlider extends React.Component {
  render() {
    return <Text>defaultValue: {this.props.defaultValue}</Text>;
  }
}

结果: View Result

【讨论】:

  • 已确定Ahelle 按预期运行,问题/问题与CustomSlider 有关。这没有解决为什么 OP 的 CustomSlider 没有重新渲染。
  • 正如@DrewReese 所说,他说它已经在工作,问题是在组件更改后重新渲染组件
猜你喜欢
  • 2021-11-24
  • 2020-06-26
  • 1970-01-01
  • 2021-06-25
  • 2018-12-31
  • 2020-04-22
  • 2017-03-26
  • 2020-01-20
  • 1970-01-01
相关资源
最近更新 更多