简体   繁体   English

在 state 更改后,React 组件不呈现

[英]React Component does not render after the state changes

Hello I have a problem with re rendering the component below you can see my code for the first screen within that I have a custom component:您好,我在重新渲染下面的组件时遇到问题,您可以在第一个屏幕中看到我的代码,其中我有一个自定义组件:

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

Here I have a custom component and I pass a default value for showing but when I change the state, it is not changing the value I passed as props.这里我有一个自定义组件,我传递了一个默认值来显示,但是当我更改 state 时,它不会更改我作为道具传递的值。 here is my custom slider component and its state, props and any other details.这是我的自定义 slider 组件及其 state、道具和任何其他细节。

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 Thank you for your time查看实际问题感谢您抽出宝贵时间

Your slider component never does anything with updated prop values that are passed.您的 slider 组件永远不会对传递的更新的 prop 值执行任何操作。 Coincidentally, it's also a react anti-pattern to store passed props in local component state.巧合的是,将传递的道具存储在本地组件 state 中也是一种反应反模式。 You can, and should, consume them directly.您可以而且应该直接食用它们。 Just pass this.state.mamad to the value prop and consume.只需将this.state.mamad传递给value道具并使用即可。 You can also pass along any additional props by spreading them in to the slider component.您还可以通过将任何其他道具传播到 slider 组件来传递它们。

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

export default Test;

Usage用法

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

If you really wanted to store the passed defaultValue prop and keep it updated, then implement the componentDidUpdate lifecycle function.如果你真的想存储传递的defaultValue并保持更新,那么实现componentDidUpdate生命周期 function。 Note, this is not the recommended solution.请注意,这不是推荐的解决方案。

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;

Your code is correct.你的代码是正确的。 I tested it.我测试了它。

App.js应用程序.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 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>;
  }
}

Result: View Result结果:查看结果

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM