setState与嵌套的JSON

时间:2017-12-07 07:35:33

标签: reactjs react-native

我是RN的新手,我正在尝试弄清楚如何将setState与嵌套的JSON一起使用。

我的对象采用以下格式:

Array[X forSome { type X <: ParentClass[A, X, M]; type M <: OtherClass[A, M] }]

我已将状态设置如下:

btn.setOnClickListener(new View.OnClickListener() {
  @Override
  public void onClick(View v) {
    LatLng location= new LatLng(34,118); // just for example

    String urlWaze = "https://waze.com/ul?ll="+location.latitude+","+location.longitude;
    Intent intentWaze = new Intent(Intent.ACTION_VIEW, Uri.parse(urlWaze));
    intentWaze.setPackage("com.waze");

    String uriGoogle = "google.navigation:q=" + location.latitude + "," + location.longitude;
    Intent intentGoogleNav = new Intent(Intent.ACTION_VIEW, Uri.parse(uriGoogle));
    intentGoogleNav.setPackage("com.google.android.apps.maps");

  }
}

我需要更新计数值并设置状态:

{
  "data": {
    "count": 1,
    "current": [
      {
        "Id":"284"
      },
      {
        "Id":"285"
      },
      {
        "Id":"286"
      }
    ]
  }
}

我正在尝试state = { notices: [data] }; ,但我无法弄清楚如何为嵌套的JSON设置它。

1 个答案:

答案 0 :(得分:1)

您可以按照以下方式执行此操作:

<form action="thank-you.php">
    <input type="text" name="name" value="" />
    <input type="text" name="email" value="<?php echo !empty($_POST['email']) ? $_POST['email'] : ''; ?>" />
    <input type="submit" value="Submit" />
</form>

然后,您可以拨打updateNotices = (newcount) => { const { notices } = this.state; const newNotices = [... notices]; newNotices[0].data.count = newcount; this.setState({ notices: newNotices }); }