FontAwesome react-native warnings - react-native

i am trying to use fontawesome in react-native-web-app
Did everything as suggested in docs, but for some reason getting this warning in console.
<FontAwesomeIcon icon={faSearch} style={[styles.icon]} /> - this is the part which triggers all warnings. If I will remove it , warning are gone too.
Warnings:
Warning: React does not recognize the secondaryFill prop on a DOM element. If you intentionally want it to appear in the DOM as a custom attribute, spell it as lowercase secondaryfill instead. If you accidentally passed it from a parent component, remove it from the DOM element.
Warning: React does not recognize the secondaryOpacity prop on a DOM element. If you intentionally want it to appear in the DOM as a custom attribute, spell it as lowercase secondaryopacity instead. If you accidentally passed it from a parent component, remove it from the DOM element.
Warning: findDOMNode is deprecated in StrictMode. findDOMNode was passed an instance of Path which is inside StrictMode. Instead, add a ref directly to the element you want to reference. Learn more about using refs safely here: https://reactjs.org/link/strict-mode-find-node
Warning: findDOMNode is deprecated in StrictMode. findDOMNode was passed an instance of Svg which is inside StrictMode. Instead, add a ref directly to the element you want to reference. Learn more about using refs safely here: https://reactjs.org/link/strict-mode-find-node

It looks like this is a known issue with an open pull request in the library: https://github.com/FortAwesome/react-native-fontawesome/pull/74
For now, the last comment in that thread offers a workaround. You can use #fortawesome/react-fontawesome for web, and react-native-fontawesome for Android/iOS.
Copying their code snippet here:
import {FontAwesomeIcon as FontAwesomeReact} from '#fortawesome/react-fontawesome';
import {FontAwesomeIcon as FontAwesomeNative} from '#fortawesome/react-native-fontawesome';
import {number} from 'prop-types';
import React from 'react';
import {Platform, StyleSheet} from 'react-native';
FaIcon.propTypes = {
size: number,
};
export default function FaIcon({size, style, ...props}) {
if (Platform.OS === 'web') {
const webStyles = StyleSheet.flatten([style, {width: size, height: size}]);
return <FontAwesomeReact {...props} style={webStyles} />;
}
return <FontAwesomeNative {...props} size={size} style={style} />;
}

Related

React Native Sortable List JSX element class does not support attributes

I am using this library: https://github.com/gitim/react-native-sortable-list ("react-native-sortable-list": "0.0.24") which does not have a type definition so I use:
"#types/react-native-sortable-list": "^0.0.9".
I have the following component defined in a tsx file (typescript):
import SortableList from 'react-native-sortable-list'
import ....// others
interface Props {
tasks: ITask[]
}
export default function TasksList(props: Props) {
return (
<SortableList
data={props.tasks}
renderRow={({ active, data, disabled, index, key }) => {
return (
<View key={key}>
<Text>{data.name}</Text>
</View>
)
}}
/>
)
}
The code works (the list gets rendered fine) but I get a Typescript error:
JSX element class does not support attributes because it does not have a 'props' property.ts(2607)
'SortableList' cannot be used as a JSX component.
Its instance type 'SortableList<unknown, unknown>' is not a valid JSX element.
Type 'SortableList<unknown, unknown>' is missing the following properties from type 'ElementClass': render, context, setState, forceUpdate, and 3 more.ts(2786)
I don't know how to make the error go away. Any idea?
I found out that Visual Studio Code had Typescript version 4.1.5. Once I changed it to 3.9.9 the error message was gone!

ReactNative scrollToEnd() on FlatList - Functional Component

Am trying to implement a chat app using ReactNative. The problem am facing is, after new item is added to the FlatList, am trying to scroll the FlatList items to the bottom so that the newly added message is visible.
When I checked the documentation, I can see a method called scrollToEnd(). But not sure how to use it as am following the Functional Component style of coding. Because when I googled, I can see examples where it uses the ref in a Class Component style coding.
But couldn't find how to use it in Functional Component!
I think you're looking for useRef
// I hope you don't mind the typescript
import {useRef} from 'react';
import {FlatList} from 'react-native';
export const Comp = () => {
const flatListRef = useRef<FlatList<any>>();
// however you detect new items
flatListRef?.current?.scrollToEnd();
return (
<FlatList
data={[]}
renderItem={() => null}
ref={flatListRef}
/>
);
}
But I think if you use inverted={true} on the flat list, it should snap to the top, or better bottom (I think).
For typescript you can use just:
const listRef = useRef<FlatList>(null);
and in your code you just need to check if your ref is exist:
listRef?.current?.scrollToIndex({animated: true, index: yourIndex})

Why does Switch in ReactNative show toggle animation although its value prop is not changed? (iOS)

I am experiencing some behavior of a ReactNative Switch that I cannot explain.
I have a very simple switch (see code below). It has a fixed value prop and its onValueChange-callback is only a log. As a switch is a controlled component, I thought the UI is only rerendered when the value prop changes.
Despite my assumptions the switch shows a quick animation of toggling to false and then jumps back to true. Why is that happening? Does it have to do with the iOS specific component that is used by ReactNative?
import React from "react";
import { Switch, View } from "react-native";
export const SwitchCell = () => {
return (
<View>
<Switch onValueChange={() => console.log("Test")} value={true} />
</View>
);
};
If you want to block the event there is a prop for switch called "onChange" where as parameter you will receive the event and the new value, you can execute a check to decide if will be necessary to set the new property o if it won't change.
In case you doesn't want to change the value of switch you have to call the methods "preventDefault()"

React Native Text.defaultProps does not exist and creating it doesn't work either

I'm new to React Native (0.59.3) and I'm trying to set default font for all Text components in my app. I've read https://stackoverflow.com/a/47925418/811405 and How to disable font scaling in React Native for IOS app?.
In my App.js I've put:
import { Text } from 'react-native';
Text.defaultProps.style = {
fontFamily: 'AmericanTypewriter' //just for testing
}
But I get Cannot set property 'style' of undefined.
When I add the line Text.defaultProps = Text.defaultProps || {}; before that, I don't get an error, but nothing happens (all Text components still use the default font). I've tried with different fonts (both built-in iOS fonts and my custom fonts that I've linked and verified), using their PostScript names, though nothing happens.
What am I doing wrong?
Add this before changing fontFamily style
import { Text } from 'react-native';
Text.defaultProps = Text.defaultProps || {};
If you want to have your own custom Text in your app, you usually create a custom Text component ... and either use it directly in your screens ... or use it internally in your other custom components ... this way you're going to have consistent look and feel throughout your app
Example
You'd use AppText in your entire app ... and forget about Text
const AppText = ({ text }) => (
<Text style={{ fontFamily: 'AmericanTypewriter', ...restOfYourStyles }}>
{text}
</Text>
);

Validate Style property type in React Native

I have an component that takes a property which contains the syling for a sub-component. I would like to ensure propTypes correctly validates it's type. I can from the React Native code that it has a ViewStylePropTypes module that provides this, however I cannot seem to find where/if it is exposed.
What I want to know is, what is the correct way of validating this without reinventing the wheel?
To enforce styling restrictions for PropTypes just use the following, dependent on what type of component you are rendering:
MyComponent.propTypes = {
/**
* Style to be applied to the containing <View>
*/
buttonStyle: View.propTypes.style,
/**
* Style to be applied to the inner <Text>
*/
textStyle: Text.propTypes.style
}
For example Text.propTypes.style will show a YellowBox warning when border is defined in thetextStyle property.
Note: This will also result in the regular Failed prop type supplied to Text... warning that occurs when rendering Text inside a component with an invalid style attribute. The propTypes validation allows your custom component to also validate this at the same time, giving you better granularity.