分类 ReactJS 下的文章

TouchableOpacity


今天学的组件有点于类似于原生Android当中的button,点击它时它的背景会发生变化,给用户一种视觉交互。在Android中叫selector,而在RN中叫Touchable系列。之前在学Text组件时,我们通过onPress事件可以给Text组件绑定触摸点击事件。为了像Text组件那样使得其他组件也可以被点击,RN提供了三个Touchable类组件:

TouchableHightlight:高亮触摸。用户点击时,会产生高亮效果
TouchableOpacity:透明触摸。用户点击时,被点击的组件会出现透明效果


接收一个事件处理器作为props


Talker.js

    var React = require('react');
    var ReactDOM = require('react-dom');
    var Button = require('./Button');
    var Talker = React.createClass({
      handleClick: function () {//一个事件处理器
        for (var speech = '', i = 0; i < 2; i++) {
          speech += 'blah ';
        }
        alert(speech);
      },
      
      render: function () {
        return <Button onClick={this.handleClick}></Button>;//Talker的属性有onClick
      }
    });
    ReactDOM.render(
      <Talker ></Talker>,
      document.getElementById('app')
    );

Button.js

var React = require('react');
var Button = React.createClass({
  render: function () {
    return (
      <button onClick={this.props.onClick}>//后面的onClick是Talker.js中的Talker组件的属性
        Click me!
      </button>
    );
  }
});
module.exports = Button;

module.exports


用来导入某个js文件中的某一个模块。

What you need is a way to import only a specific part of a file into another file.

The answer is something called module.exports.module.exports comes from Node.js's module system, just like require does.

NOTE:Any component rendered by a different component must be included in module.exports.

Remember that every file is invisible to every other file, except for what you choose to expose via module.exports.
NavBar.js

var React = require('react');
var NavBar = React.createClass({
  render: function () {
    var pages = ['home', 'blog', 'pics', 'bio', 'art', 'shop', 'about', 'contact'];
    var navLinks = pages.map(function(page){
      return (
        <a href={'/' + page}>
          {page}
        </a>
      );
    });
    return <nav>{navLinks}</nav>;
  }
});
module.exports=NavBar;//

module.exports=NavBar;加上后,
Now, when ProfilePage.js uses requireto import NavBar.js, it will get back exactly what it wants: the NavBar component class.

ProfilePage.js
var React = require('react');
var ReactDOM = require('react-dom');
var NavBar = require('./NavBar');
var ProfilePage = React.createClass({
  render: function () {
    return (
      <div>
<NavBar />
        <h1>All About Me!</h1>
        <p>I like movies and blah blah blah blah blah</p>
        <img src="https://s3.amazonaws.com/codecademy-content/courses/React/react_photo-monkeyselfie.jpg" />
      </div>
    );
  }
});

A curmudgeonly clarification about grammar


You may have noticed some loose usage of the words prop and props. Unfortunately this is pretty inevitable.

props is the name of the object that stores passed-in information. this.props refers to that storage object. At the same time, each piece of passed-in information is also called a prop. props could refer to two pieces of passed-in information, or it could refer to the object that stores those pieces of information. :(


shijiebei 365bet manbetx 188bet xinshui caipiao 95zz tongbaoyule beplay 88bifa 18luck betway bwin hg0088 aomenjinshayulecheng ca88 shenbotaiyangcheng vwin w88 weide