React Native: 如何在用户输入后生成一个包含用户输入文本的框?

huangapple go评论111阅读模式
英文:

React Native: How do I generate a box after user input with the text from user input?

问题

我怎样在用户输入后生成一个包含用户输入文本(已选择、数量、度量、日、月、年)的框?

我也希望在导航栈的另一页显示和生成此框,但我假设我可以使用 "props" 关键字并在其他页面初始化此功能(AddFood)来实现这一点?

这是整个函数的代码(不过重要部分如下):

// 手动添加食物
const AddFood = (props) => {
    
    // 下拉菜单常量
    const [selected, setSelected] = React.useState("");
    
    // 数量常量
    const [quantity, setQuantity] = React.useState("");
    const [metric, setMetric] = React.useState("");
    
    // 日期常量
    const [day, setDay] = React.useState("");
    const [month, setMonth] = React.useState("");
    const [year, setYear] = React.useState("");
    
    // 下拉菜单数据
    const data = [
        { key: 'Test1', value: 'Test1' },
        { key: 'Test2', value: 'Test2' },
        { key: 'Test3', value: 'Test3' },
    ]

    const metricData = [
        { key: 'ml', value: 'ml' },
        { key: 'g', value: 'g' },
        { key: 'litres', value: 'litres' },
    ]

    const dayData = [
        { key: '1', value: '01' },
    ]

    const monthData = [
        { key: '1', value: '01' },
    ]

    const yearData = [
        { key: '2023', value: '2023' },
    ]

    const onPressBack = () => {
        props.navigation.navigate('BarcodeOrManual');
    }

    // 验证并将数据传递到数据库
    const showAlert = () => {
        if (selected && quantity && metric && day && month && year) {

            Alert.alert(
                "食物成功输入"
            );

            // 在这里传递数据

        } else {

            Alert.alert(
                "请填写所有字段"
            );

        }
    }

    return (
        <View style={{ paddingHorizontal: 20, paddingVertical: 50, flex: 1 }}>

            {/* 食物下拉菜单 */}
            <SelectList
                data={data}
                // onSelect={() => alert(selected)}
                // setSelected={setSelected} 
                setSelected={(val) => setSelected(val)}
                dropdownItemStyles={{ marginHorizontal: 10 }}
                dropdownTextStyles={{ color: 'black' }}
                searchPlaceholder="输入项目"
                placeholder="输入项目"
                maxHeight={100}
            />

            {/* 食物数量 */}
            <Text>数量</Text>
            <View style={{ flexDirection: "row" }}>
                <TextInput
                    style={styles.input}
                    onChangeText={setQuantity}
                    value={quantity}
                    keyboardType="numeric"
                />
                <SelectList
                    data={metricData}
                    // onSelect={() => alert(selected)}
                    // setSelected={setSelected} 
                    setSelected={(val) => setMetric(val)}
                    dropdownItemStyles={{ marginHorizontal: 10 }}
                    dropdownTextStyles={{ color: 'black' }}
                    searchPlaceholder="(ml, g, 等)"
                    placeholder="输入度量"
                    maxHeight={100}
                />
            </View>

            {/* 日期 */}
            <Text>使用日期</Text>
            <View style={{ flexDirection: "row" }}>
                <SelectList
                    data={dayData}
                    // onSelect={() => alert(selected)}
                    // setSelected={setSelected} 
                    setSelected={(val) => setDay(val)}
                    dropdownItemStyles={{ marginHorizontal: 10 }}
                    dropdownTextStyles={{ color: 'black' }}
                    placeholder=" "
                    maxHeight={100}
                />
                <Text>DD /</Text>
                <SelectList
                    data={monthData}
                    // onSelect={() => alert(selected)}
                    // setSelected={setSelected} 
                    setSelected={(val) => setMonth(val)}
                    dropdownItemStyles={{ marginHorizontal: 10 }}
                    dropdownTextStyles={{ color: 'black' }}
                    placeholder=" "
                    maxHeight={100}
                />
                <Text>MM /</Text>
                <SelectList
                    data={yearData}
                    // onSelect={() => alert(selected)}
                    // setSelected={setSelected} 
                    setSelected={(val) => setYear(val)}
                    dropdownItemStyles={{ marginHorizontal: 10 }}
                    dropdownTextStyles={{ color: 'black' }}
                    placeholder=" "
                    maxHeight={100}
                />
                <Text>YYYY /</Text>
            </View>

            <Pressable onPress={showAlert} style={styles.button}>
                <Text style={{ fontSize: 15, color: 'white' }}>添加项目</Text>
            </Pressable>

            <Pressable onPress={onPressBack} style={styles.button}>
                <Text style={{ fontSize: 15, color: 'white' }}>返回</Text>
            </Pressable>

        </View>
    )
}

下面的变量是要显示的数据存储的地方:

// 下拉菜单常量
const [selected, setSelected] = React.useState("");

// 数量常量
const [quantity, setQuantity] = React.useState("");
const [metric, setMetric] = React.useState("");

// 日期常量
const [day, setDay] = React.useState("");
const [month, setMonth] = React.useState("");
const [year, setYear] = React.useState("");

当用户按下按钮时运行的代码如下:

// 验证并将数据传递到数据库
const showAlert = () => {
    if (selected && quantity && metric && day && month && year) {

        Alert.alert(
            "食物成功输入"
        );

        // 在这里传递数据

    } else {

        Alert.alert(
            "请填写所有字段"
        );

    }
}

谢谢!

英文:

How do I generate a box after user input with the text from user input (selected, quantity, metric, day, month, year)?

I was also hoping to display and generate this box on another page in the Navigation stack but I am assuming I can do this by using the "props" keyword and initialising this function (AddFood) in the other page?

This is my code for the whole function (however the important parts are below):

// MANUALLY ADD FOOD
const AddFood = (props) =&gt; {
// DROPDOWN MENU CONST
const [selected, setSelected] = React.useState(&quot;&quot;);
// QUANTITY CONST
const [quantity, setQuantity] = React.useState(&quot;&quot;);
const [metric, setMetric] = React.useState(&quot;&quot;);
// DATE CONST
const [day, setDay] = React.useState(&quot;&quot;);
const [month, setMonth] = React.useState(&quot;&quot;);
const [year, setYear] = React.useState(&quot;&quot;);
// DROPDOWN MENU DATA
const data = [
{key:&#39;Test1&#39;,value:&#39;Test1&#39;},
{key:&#39;Test2&#39;,value:&#39;Test2&#39;},
{key:&#39;Test3&#39;,value:&#39;Test3&#39;},
]
const metricData = [
{key:&#39;ml&#39;,value:&#39;ml&#39;},
{key:&#39;g&#39;,value:&#39;g&#39;},
{key:&#39;litres&#39;,value:&#39;litres&#39;},
]
const dayData = [
{key:&#39;1&#39;,value:&#39;01&#39;},
]
const monthData = [
{key:&#39;1&#39;,value:&#39;01&#39;},
]
const yearData = [
{key:&#39;2023&#39;,value:&#39;2023&#39;},
]
const onPressBack = () =&gt; {
props.navigation.navigate(&#39;BarcodeOrManual&#39;);
}
// VALIDATION AND PASS DATA TO DATABASE
const showAlert = () =&gt; {
if (selected &amp;&amp; quantity &amp;&amp; metric &amp;&amp; day &amp;&amp; month &amp;&amp; year) {
Alert.alert (
&quot;Food entered successfully&quot;
);
// Pass data here
} else {
Alert.alert (
&quot;Please fill in all the fields&quot;
);
}
}
return (
&lt;View style={{ paddingHorizontal: 20, paddingVertical: 50, flex: 1 }}&gt;
{/* DROPDOWN MENU FOR FOOD */}
&lt;SelectList
data={data}
// onSelect={() =&gt; alert(selected)}
// setSelected={setSelected} 
setSelected={(val) =&gt; setSelected(val)}
dropdownItemStyles={{marginHorizontal:10}}
dropdownTextStyles={{color:&#39;black&#39;}}
searchPlaceholder=&quot;Enter item&quot;
placeholder=&quot;Enter item&quot;
maxHeight={100}
/&gt;
{/* FOOD QUANTITY */}
&lt;Text&gt;Quantity&lt;/Text&gt;
&lt;View style={{ flexDirection: &quot;row&quot; }}&gt;
&lt;TextInput
style={styles.input}
onChangeText={setQuantity}
value={quantity}
keyboardType=&quot;numeric&quot;
/&gt;
&lt;SelectList
data={metricData}
// onSelect={() =&gt; alert(selected)}
// setSelected={setSelected} 
setSelected={(val) =&gt; setMetric(val)}
dropdownItemStyles={{ marginHorizontal:10 }}
dropdownTextStyles={{ color:&#39;black&#39; }}
searchPlaceholder=&quot;(ml, g, etc.)&quot;
placeholder=&quot;Enter metric&quot;
maxHeight={100}
/&gt;
&lt;/View&gt;
{/* DATE */}
&lt;Text&gt;Use by date&lt;/Text&gt;
&lt;View style={{ flexDirection: &quot;row&quot; }}&gt;
&lt;SelectList
data={dayData}
// onSelect={() =&gt; alert(selected)}
// setSelected={setSelected} 
setSelected={(val) =&gt; setDay(val)}
dropdownItemStyles={{marginHorizontal:10}}
dropdownTextStyles={{color:&#39;black&#39;}}
placeholder=&quot; &quot;
maxHeight={100}
/&gt;
&lt;Text&gt;DD /&lt;/Text&gt;
&lt;SelectList
data={monthData}
// onSelect={() =&gt; alert(selected)}
// setSelected={setSelected} 
setSelected={(val) =&gt; setMonth(val)}
dropdownItemStyles={{marginHorizontal:10}}
dropdownTextStyles={{color:&#39;black&#39;}}
placeholder=&quot; &quot;
maxHeight={100}
/&gt;
&lt;Text&gt;MM /&lt;/Text&gt;
&lt;SelectList
data={yearData}
// onSelect={() =&gt; alert(selected)}
// setSelected={setSelected} 
setSelected={(val) =&gt; setYear(val)}
dropdownItemStyles={{marginHorizontal:10}}
dropdownTextStyles={{color:&#39;black&#39;}}
placeholder=&quot; &quot;
maxHeight={100}
/&gt;
&lt;Text&gt;YYYY /&lt;/Text&gt;
&lt;/View&gt;
&lt;Pressable onPress={showAlert} style={styles.button}&gt;
&lt;Text style={{ fontSize: 15, color: &#39;white&#39; }}&gt;Add Item&lt;/Text&gt;
&lt;/Pressable&gt;
&lt;Pressable onPress={onPressBack} style={styles.button}&gt;
&lt;Text style={{ fontSize: 15, color: &#39;white&#39; }}&gt;BACK&lt;/Text&gt;
&lt;/Pressable&gt;
&lt;/View&gt;
)
// VARIABLE STORES:
// selected -&gt; Food dropdown menu selection
// quantity -&gt; Quantity of food
// metric -&gt; The relevant metric for the quantity of food
// day, month, year -&gt; Use by date
}

The variables below is where the data I want to display is stored:

// DROPDOWN MENU CONST
const [selected, setSelected] = React.useState(&quot;&quot;);
// QUANTITY CONST
const [quantity, setQuantity] = React.useState(&quot;&quot;);
const [metric, setMetric] = React.useState(&quot;&quot;);
// DATE CONST
const [day, setDay] = React.useState(&quot;&quot;);
const [month, setMonth] = React.useState(&quot;&quot;);
const [year, setYear] = React.useState(&quot;&quot;);

This is the code that is run when the button is pressed by the user:

// VALIDATION AND PASS DATA TO DATABASE
const showAlert = () =&gt; {
if (selected &amp;&amp; quantity &amp;&amp; metric &amp;&amp; day &amp;&amp; month &amp;&amp; year) {
Alert.alert (
&quot;Food entered successfully&quot;
);
// Pass data here
} else {
Alert.alert (
&quot;Please fill in all the fields&quot;
);
}
}

Thank you!

答案1

得分: 2

如果你正在使用React Navigation,当你导航到其他页面时,你可以将这些变量作为参数传递。

  const { navigate } = useNavigation();
  navigate('otherScreen', { quantity, metric, day, month, year });

在你的其他页面中:

  const { params } = useRoute();
  console.log(params);

查看 https://reactnavigation.org/docs/params/

英文:

If you're using React Navigation, you can pass these variables as params when you navigate to the other page.

  const { navigate } = useNavigation();
  navigate(&#39;otherScreen&#39;, { quantity, metric, day, month, year });

and in your other screen

  const { params } = useRoute();
  console.log(params);

See https://reactnavigation.org/docs/params/

huangapple
  • 本文由 发表于 2023年2月27日 01:46:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/75573896.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定