簡體   English   中英

如何根據用戶輸入在屏幕上顯示 json 數據 expo react-native

[英]How to display json data on screen based on user input expo react-native

我有一些從 api 獲取的 json 數據,並根據用戶輸入顯示在 iOS 模擬器上。

屏幕頂部有一個搜索欄,用戶可以在其中鍵入任何符號值,並且應該在屏幕上顯示該特定值。

我的 json 數據:

 Object {
    "name": "Chesapeake Energy",
    "symbol": "CHK",
  },
  Object {
    "name": "C. H. Robinson Worldwide",
    "symbol": "CHRW",
  },

因此,如果用戶鍵入 CHK,它應該顯示符號和名稱,如下所示:

CHK
Chesapeake Energy

我的代碼:

import React, { useState, useEffect } from "react";
import {
  StyleSheet,
  View,
  TouchableWithoutFeedback,
  Keyboard,
  FlatList,
  TextInput,
  Button,
  Text,
} from "react-native";
import { useStocksContext } from "../contexts/StocksContext";
import { scaleSize } from "../constants/Layout";
import { Ionicons } from "@expo/vector-icons";

import { ListItem } from "react-native";

export default function SearchScreen({ navigation }) {
  const { ServerURL, addToWatchlist } = useStocksContext();

  const [state, setState] = useState({
    /*  initial state here */
    myListData: [],
    search: "",
  });



  useEffect(() => {
    renderWithData();

  }, []);

  renderWithData = () => {
    return fetch("http://131.181.190.87:3001/all")
      .then((res) => res.json())
      .then((json) => {
        setState({
          isLoaded: true,
          myListData: json,
        });
        setTimeout(() => {
          console.log(json);
        }, 1000);
      });
  };

  let onChangeText = (event) => {
    setState({ search: event }); //here it does not set the value of event(userinput) to search

  };



  return (
    <TouchableWithoutFeedback onPress={Keyboard.dismiss}>
      <View style={styles.container}>
        <TextInput
          style={styles.textinput}
          placeholder="Search here"
          placeholderTextColor="white"
          onChangeText={(search) => onChangeText(search)}
        />


      </View>
    </TouchableWithoutFeedback>
  );
}

我的問題是如何編寫 onChangeText() 以便可以根據用戶輸入在屏幕上顯示符號和名稱。

 let onChangeText = (event) => {
        setState({ search: event }); //here it does not set the value of event(userinput) to search

      };

在這里,我試圖將用戶值(事件)設置為search ,但它沒有設置值,但如果我打印event ,我可以在控制台中看到任何用戶類型。 如何根據用戶類型顯示符號和名稱? 有沒有辦法過濾這些?

是的。 javascript中有一個filter功能。

首先,您必須將 JSON 用戶列表存儲在 state 或某處,例如

userlist: []

然后你的onChangeText

 onChangeText = {(event) => { let result = userlist.filter((item) => { return item.symbol === event }); // result is an array contains your users that have symbol in your input. // Handle your result. }}

你可以在這里找到更多:

https://developer.mozilla.org/vi/docs/Web/JavaScript/Reference/Global_Objects/Array/filter

那它。 希望有所幫助。 :)

React-native 通過 Json 值反應原生<input< div><div id="text_translate"><h2> <strong>大家好,</strong></h2><p> 我只是從 react-native 開始為 android cel 創建一個應用程序。 我有一個包含 2 個字段 / 的表單,我想從 json 加載這些字段,這些字段是從 php 文件中提取的。 如果我只通過 json 發送一個值,我可以使用“componentDidMount”和“XMLHttpRequest()”在一個字段中加載該值這是我在 componentdidmount 使用的代碼:</p><pre> componentDidMount=()=&gt; { let _this= this; const xhttp = new XMLHttpRequest(); xhttp.onreadystatechange = function() { if (this.readyState == 4 &amp;&amp; this.status == 200){ console.log(this.responseText); var temp = JSON.parse(xhttp.responseText); console.log(temp); _this.setState({datos: temp}); } }; xhttp.open('GET', 'https://smppage.000webhostapp.com/gest_ObtFolio2.php', true); xhttp.send(); }</pre><p> 這些是 console.log 中打印的值: responsetext: LOG [{"iddeOS":"13"},{"fecha":"2021-12-04"}] temp: LOG [{"iddeOS": "13 "}, {"fecha": "2021-12-04"}]</p><p> 當我只通過 json 發送一個字段時,我可以將它加載到一個字段中,但我想通過 json 發送 2 個字段,這是我通過 json 發送的值,僅用於一個字段:LOG14:響應文本:是我在一個中使用的代碼/當我只拉一個字段並且該字段正確加載到我的</p><pre> &lt;View style={{flex:1}}&gt; &lt;Input ref={component =&gt; this.folioOrden = component} label=" Folio" value={this.state.datos.toString()}</pre><p> 使用此代碼,它可以正確地將值加載到一個字段中</p><p>你能幫忙找到代碼來同時填充“Folio”和“Fecha”這兩個字段並安裝組件嗎?</p><pre> &lt;View style={{flex:1}}&gt; &lt;Input ref={component =&gt; this.folioOrden = component} label=" Folio" value={this.state.datos.toString()} &lt;View style={{flex:2}}&gt; &lt;Input ref={component =&gt; this.fechaAperOrden = component} label=" Fecha"</pre><p> 我已經嘗試發送 2 個字段並在兩個輸入中使用此代碼,但我顯示錯誤:</p><pre> value={this.state.datos.iddeOS.toString()} value={this.state.datos.fecha.toString()}</pre><p> 提前致謝</p></div></input<>

[英]React-native pass Json values to react native <input

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

相關問題 如何訪問 json 數據的值 0 (react-native) expo 如何在沒有列表的情況下在 react-native 中顯示 JSON 數據? React-Native:在ListView中顯示JSON數據 在React-Native中顯示JSON數據而不使用列表? 如何根據用戶輸入過濾和顯示JSON數據? React-Native:在json中搜索輸入值 如何從 fetch (react-native) 中獲取 JSON 數據 React-native 通過 Json 值反應原生<input< div><div id="text_translate"><h2> <strong>大家好,</strong></h2><p> 我只是從 react-native 開始為 android cel 創建一個應用程序。 我有一個包含 2 個字段 / 的表單,我想從 json 加載這些字段,這些字段是從 php 文件中提取的。 如果我只通過 json 發送一個值,我可以使用“componentDidMount”和“XMLHttpRequest()”在一個字段中加載該值這是我在 componentdidmount 使用的代碼:</p><pre> componentDidMount=()=&gt; { let _this= this; const xhttp = new XMLHttpRequest(); xhttp.onreadystatechange = function() { if (this.readyState == 4 &amp;&amp; this.status == 200){ console.log(this.responseText); var temp = JSON.parse(xhttp.responseText); console.log(temp); _this.setState({datos: temp}); } }; xhttp.open('GET', 'https://smppage.000webhostapp.com/gest_ObtFolio2.php', true); xhttp.send(); }</pre><p> 這些是 console.log 中打印的值: responsetext: LOG [{"iddeOS":"13"},{"fecha":"2021-12-04"}] temp: LOG [{"iddeOS": "13 "}, {"fecha": "2021-12-04"}]</p><p> 當我只通過 json 發送一個字段時,我可以將它加載到一個字段中,但我想通過 json 發送 2 個字段,這是我通過 json 發送的值,僅用於一個字段:LOG14:響應文本:是我在一個中使用的代碼/當我只拉一個字段並且該字段正確加載到我的</p><pre> &lt;View style={{flex:1}}&gt; &lt;Input ref={component =&gt; this.folioOrden = component} label=" Folio" value={this.state.datos.toString()}</pre><p> 使用此代碼,它可以正確地將值加載到一個字段中</p><p>你能幫忙找到代碼來同時填充“Folio”和“Fecha”這兩個字段並安裝組件嗎?</p><pre> &lt;View style={{flex:1}}&gt; &lt;Input ref={component =&gt; this.folioOrden = component} label=" Folio" value={this.state.datos.toString()} &lt;View style={{flex:2}}&gt; &lt;Input ref={component =&gt; this.fechaAperOrden = component} label=" Fecha"</pre><p> 我已經嘗試發送 2 個字段並在兩個輸入中使用此代碼,但我顯示錯誤:</p><pre> value={this.state.datos.iddeOS.toString()} value={this.state.datos.fecha.toString()}</pre><p> 提前致謝</p></div></input<> 如何在FlatList React Native中顯示JSON數據 使用React-Native從JSON提取數據
 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM