简体   繁体   English

里面的console.log<errormessage> 零件</errormessage>

[英]console.log inside <ErrorMessage> component

Generally, this is how Formik's <ErrorMessage> tag is used:通常,Formik 的<ErrorMessage>标签是这样使用的:

  <ErrorMessage
                        name="email"
                        render={(msg) => (
                          <Text style={styles.errorText}>
                            {msg}
                          </Text>
                        )}
                      />

Is there any way to console.log and see the error message from inside it?有没有办法 console.log 并从里面查看错误信息? I tried making a separate function which does the printing but calling it instead of <Text> doesn't work.我尝试制作一个单独的 function 进行打印但调用它而不是<Text>不起作用。 Do I have an alternative?我有替代方案吗?

Try this尝试这个

<ErrorMessage
  name="email"
  render={(msg) => {
    console.log(msg);
    return <Text style={styles.errorText}>{msg}</Text>;
  }}
/>;

As per REACT, "At the end its all javascript" ,根据 REACT, “最后都是 javascript”

So, you can try out this:所以,你可以试试这个:

{(msg) => {
    console.log(msg); // <----- HERE
    return (<Text style={styles.errorText}>
    {msg}
    </Text>)
}}

The most consise way of doing this is to call console.log() and use the or operator with the Text component.最简洁的方法是调用console.log()并将or运算符与Text组件一起使用。 This will call console.log and will render the component - since logging returns undefined the second statement will always be executed.这将调用console.log并呈现组件 - 因为日志记录返回undefined ,所以将始终执行第二条语句。

 <ErrorMessage
    name="email"
    render={
      (msg) => console.log(msg) || <Text style={styles.errorText}>{msg}</Text>
    }
 />

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

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