登录
首页 >  文章 >  前端

作为 ReactJS 开发者如何开始使用 React Native?

来源:dev.to

时间:2024-07-20 08:18:45 312浏览 收藏

今天golang学习网给大家带来了《作为 ReactJS 开发者如何开始使用 React Native?》,其中涉及到的知识点包括等等,无论你是小白还是老手,都适合看一看哦~有好的建议也欢迎大家在评论留言,若是看完有所收获,也希望大家能多多点赞支持呀!一起加油学习~

作为 ReactJS 开发者如何开始使用 React Native?

最近,我在 react nexus 上发表了关于“辅助功能和电视应用程序”的演讲。我不断收到的一个问题是:“作为一名 reactjs 开发人员,开始使用 react native 有多容易?”

简而言之,对于 reactjs 开发者来说,从 react native 开始会很容易。

在这篇博客中,我将分享 reactjs 开发人员可以在 react native 中使用的五件事。

1. 组件

在 react native 中,您将像在 reactjs 中一样创建组件。概念和最佳实践保持不变。

import react from 'react';
import { view, text } from 'react-native';

const greetingcomponent = () => {
  return (
    <view>
      <text>hello, neha!</text>
    </view>
  );
};
export default greetingcomponent;

2. 道具和状态

在 react native 中,props 和 state 的工作方式与 reactjs 中相同。要在组件之间进行通信,您将使用 props。要更新值,您将使用状态。

import react from 'react';
import { view, text } from 'react-native';

const greetingcomponent = ({ name }) => {
  return (
    <view>
      <text>hello, {name}!</text>
    </view>
  );
};
export default greetingcomponent;

3. 挂钩

就像在 reactjs 中一样,您可以使用 react native 中的所有钩子,例如 usestate()、usememo()、useeffect() 等。此外,您还可以创建自己的自定义钩子。

import react, { usestate } from 'react';
import { view, text, button, stylesheet } from 'react-native';

const greetingcomponent = () => {
  const [name, setname] = usestate('john');

  const changename = () => {
    setname('jane');
  };

  return (
    <view style={styles.container}>
      <text>hello, {name}!</text>
      <button title="change name" onpress={changename} />
    </view>
  );
};

export default greetingcomponent;

4. 测试

如果您是 react 测试库的粉丝,好消息是您可以使用相同的库在 react native 中进行测试。

import react from 'react';
import { render, fireevent } from '@testing-library/react-native';
import greetingcomponent from './greetingcomponent';

test('it renders correctly and changes name on button press', () => {
  // render the component
  const { getbytext } = render(<greetingcomponent />);

  // assert initial state
  expect(getbytext('hello, john!')).tobetruthy();

  // find the button and simulate a press
  const button = getbytext('change name');
  fireevent.press(button);

  // assert that the name has changed
  expect(getbytext('hello, jane!')).tobetruthy();
});

5.jsx

在 react native 中,有一些组件可用于在 jsx 中创建视图。但是,在 reactjs 中,您可以使用任何有效的 html dom 元素。

import react from 'react';
import { view, text } from 'react-native';

const greetingcomponent = () => {
  return (
    <view>
      <text>hello, neha!</text>
    </view>
  );
};
export default greetingcomponent;

快乐学习!!

理论要掌握,实操不能落!以上关于《作为 ReactJS 开发者如何开始使用 React Native?》的详细介绍,大家都掌握了吧!如果想要继续提升自己的能力,那么就来关注golang学习网公众号吧!

声明:本文转载于:dev.to 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>