javascript - 更改屏幕后 react native 保存按钮状态

标签 javascript reactjs react-native

我的应用程序中有 5 个按钮 [“运行”、“骑行”、“阅读”、“编码”、“牛儿”],当我点击它时,按钮会改变颜色并在屏幕上显示标题。我正在使用这个库:react-native-selectmultiple-button .

假设我点击了“运行”和“骑行”按钮,这些按钮将突出显示并且文本将显示在屏幕上,但是当我将屏幕切换到另一页并返回到上一个屏幕时,按钮状态将设置回默认值.

下面是我的代码:

const multipleData = ["running", "riding", "reading", "coding", "Niuer"];

export default class SimpleButton extends Component {
  constructor(props) {
    super(props);
    this.state = {
      multipleSelectedDataLimited: []
    };
  }

  render() {
    return (
      <View style={{paddingTop:200}}>
      <Text style={styles.welcome}>
        implement the multiple-select buttons demo by SelectMultipleButton
      </Text>
      <Text style={{ color: 'blue', marginLeft: 10 }}>
      I like {_.join(this.state.multipleSelectedDataLimited, ", ")}
      </Text>
        <View
          style={{
            flexWrap: "wrap",
            flexDirection: "row",
            justifyContent: "center"
          }}
        >
          {multipleData.map(interest => (
            <SelectMultipleButton
              key={interest}
              buttonViewStyle={{
                borderRadius: 10,
                height: 40
              }}
              textStyle={{
                fontSize: 15
              }}
              highLightStyle={{
                borderColor: "gray",
                backgroundColor: "transparent",
                textColor: "gray",
                borderTintColor: 'blue',
                backgroundTintColor: 'blue',
                textTintColor: "white"
              }}
              value={interest}
              selected={this.state.multipleSelectedDataLimited.includes(
                interest
              )}
              singleTap={valueTap =>
                this._singleTapMultipleSelectedButtons_limited(interest)
              }
            />
          ))}
        </View>
      </View>
    );
  }

  _singleTapMultipleSelectedButtons_limited(interest) {
    if (this.state.multipleSelectedDataLimited.includes(interest)) {
      _.remove(this.state.multipleSelectedDataLimited, ele => {
        return ele === interest;
      });
    } else {
      if (this.state.multipleSelectedDataLimited.length < 3)
        this.state.multipleSelectedDataLimited.push(interest);
    }
    this.setState({
      multipleSelectedDataLimited: this.state.multipleSelectedDataLimited
    });
  }
}

const styles = StyleSheet.create({
  welcome: {
    margin: 10,
    marginTop: 30,
    color: "gray"
  }
});

有没有办法在更换屏幕后保持按钮的状态?

如有任何建议或意见,我们将不胜感激。提前致谢!

最佳答案

你能做的最好的事情就是将你的状态保存在 Redux 中并使用 redux-persist 。您还可以使用 AsyncStorage。我有类似的情况,我必须保持两个组件来回导航之间的状态,我使用了这样的导航参数:

屏幕 A:

this.props.navigation.navigate('ScreenB', {
              onPressScreenAFun: (params) => {
                this.screenAFun(params),
                ButtonState1: true // you can send multiple params
              },
            })

screenAFun = (ButtonState1) => {
 this.setState({buttonState1: ButtonState1})
}

屏幕 B:

    screenBFun = (params) => {
       const { onPressScreenAFun, ButtonState1 } = this.props.navigation.navigate.state.params

      onPressScreenAFun(ButtonState1)
      this.props.navigation.goBack()
    }

关于javascript - 更改屏幕后 react native 保存按钮状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52732800/

相关文章:

javascript - 如果其中的单元格包含特定单词,则隐藏表格行

javascript - 是否有任何有意义的用例将一个函数对象的实例分配给另一个函数对象的原型(prototype)属性?

javascript - React JS this.state未定义: Cannot read property 'components' of undefined,,其中组件是 "Board"组件的状态

javascript - 如果我在屏幕上来回移动,为什么 useState 的状态会突然恢复为 false?

android - 如何启用 stacktrace react-native run-android 命令?

Javascript:for 循环检索并打印一定量的 API 数据

javascript - 如何始终获得良好的页面源格式?

javascript - 当数组 prop 更改时,React 子组件不会重新渲染

node.js - `npm start` 给出生命周期错误并以状态 9 退出

ios - React-Native iOS 热重载和 Debug模式在真实设备中不起作用