K
K
KnightForce2019-03-01 17:21:11
Android
KnightForce, 2019-03-01 17:21:11

React Native. Why is the Android component not rendering?

I want to add a component from Android to React Native. But it doesn't render and doesn't have dimensions. How can I fix this?
I used other people's guides and examples. Classes to connect the component:
CustomViewManager.java:

public class CustomViewManager extends SimpleViewManager<TextView> {

public static final String REACT_CLASS = "CustomView";

@Override
public String getName() {
    return REACT_CLASS;
}

@Override
public TextView createViewInstance(ThemedReactContext context) {

    TextView tv = new TextView(context);
    tv.setText("Текст");
    tv.setTextColor(Color.BLACK);
    tv.setWidth(100);
    tv.setHeight(100);
    return tv;

  }
}

CustomViewPackage.java:
public class CustomViewPackage implements ReactPackage {

@Override
public List<ViewManager>
createViewManagers(ReactApplicationContext reactContext) {
    return Arrays.<ViewManager>asList(
            new CustomViewManager()
    );
}

@Override
public List<NativeModule>
createNativeModules(ReactApplicationContext reactContext) {
    return Collections.emptyList();
}

}

MainApplication.java:
@Override
protected List<ReactPackage> getPackages() {
  return Arrays.<ReactPackage>asList(
      new MainReactPackage(),
      new CustomViewPackage()
  );
}

CustomTextView.js:
import React from 'react';
import {
   requireNativeComponent
} from 'react-native';

module.exports = requireNativeComponent('CustomView');

app.js:
export default class App extends Component {
render() {
  return (
    <View 
      style={ styles.container }
    >
      <CustomView/>
    </View>
  );
}
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
K
KnightForce, 2019-03-10
@KnightForce

It turned out you need to set the dimensions through Flex.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question