<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>React &#8211; 编程技术记录</title>
	<atom:link href="https://blog.z6z8.cn/category/%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0/react/feed/" rel="self" type="application/rss+xml" />
	<link>https://blog.z6z8.cn</link>
	<description>世界你好!</description>
	<lastBuildDate>Tue, 15 Jun 2021 02:29:22 +0000</lastBuildDate>
	<language>zh-Hans</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.8.3</generator>
	<item>
		<title>设置环境变量关闭react-scripts的sourceMap生成</title>
		<link>https://blog.z6z8.cn/2021/06/15/%e8%ae%be%e7%bd%ae%e7%8e%af%e5%a2%83%e5%8f%98%e9%87%8f%e5%85%b3%e9%97%adreact-scripts%e7%9a%84sourcemap%e7%94%9f%e6%88%90/</link>
		
		<dc:creator><![CDATA[holdsky]]></dc:creator>
		<pubDate>Tue, 15 Jun 2021 02:29:22 +0000</pubDate>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[React]]></category>
		<category><![CDATA[学习笔记]]></category>
		<guid isPermaLink="false">http://blog.z6z8.cn/?p=1052</guid>

					<description><![CDATA[关于GENERATE_SOURCEMAP的作用原理在这篇文章中介绍过。 http://139.155.43.7 [&#8230;]]]></description>
										<content:encoded><![CDATA[<p>关于<code>GENERATE_SOURCEMAP</code>的作用原理在这篇文章中介绍过。<br />
<a href="http://139.155.43.7/2019/10/15/create-react-app-%ef%bc%9a%e4%b8%8d%e4%ba%a7%e7%94%9fsource-map%e6%9d%a5%e7%bc%a9%e5%87%8f%e6%89%93%e5%8c%85%e6%96%87%e4%bb%b6%e4%bd%93%e7%a7%af/">http://139.155.43.7/2019/10/15/create-react-app-%ef%bc%9a%e4%b8%8d%e4%ba%a7%e7%94%9fsource-map%e6%9d%a5%e7%bc%a9%e5%87%8f%e6%89%93%e5%8c%85%e6%96%87%e4%bb%b6%e4%bd%93%e7%a7%af/</a></p>
<p>可以通过创建<code>.env</code>环境变量文件影响<code>react-scripts start/build</code>等命令。其实还有另一种更灵活的方式设置环境变量,使用<code>export</code>可以对不同的命令设置不同的环境变量.</p>
<p>打开<code>package.json</code>，找到<code>scripts</code>配置项</p>
<pre><code class="language-json">  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject"
  },</code></pre>
<p>添加<code>export GENERATE_SOURCEMAP=true/false</code>修改为以下配置</p>
<pre><code class="language-json">  "scripts": {
    "start": "export GENERATE_SOURCEMAP=true && react-scripts start",
    "build": "export GENERATE_SOURCEMAP=false && react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject"
  },</code></pre>
<p>这样，在运行start调试时，产生sourceMap方便调试；运行build命令时，关闭sourceMap减少产物文件大小。</p>
]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>TS2769: Property &#8216;xxx&#8217; does not exist on type &#8216;IntrinsicAttributes &#038; IntrinsicClassAttributes &#038; Readonly &#038; Readonly</title>
		<link>https://blog.z6z8.cn/2020/04/05/ts2769-property-xxx-does-not-exist-on-type-intrinsicattributes-intrinsicclassattributes-readonly-readonly/</link>
		
		<dc:creator><![CDATA[holdsky]]></dc:creator>
		<pubDate>Sun, 05 Apr 2020 04:01:03 +0000</pubDate>
				<category><![CDATA[React]]></category>
		<category><![CDATA[学习笔记]]></category>
		<guid isPermaLink="false">http://blog.z6z8.cn/?p=850</guid>

					<description><![CDATA[用TypeScript开发React项目，在父子组件间传值时发生错误提示 class Page extends [&#8230;]]]></description>
										<content:encoded><![CDATA[<p>用TypeScript开发React项目，在父子组件间传值时发生错误提示</p>
<pre><code class="language-ts">class Page extends React.Component
{
    render()
    {
        return &lt;div&gt;
                &lt;NavigationBar title="标题"/&gt;
      &lt;/div&gt;
    }
}

class NavigationBar extends React.Component
{
    render()
    {
        return &lt;div&gt;
            "返回标题"
        &lt;/div&gt;
    }
}</code></pre>
<p>错误提示</p>
<pre><code class="language-shell">
No overload matches this call.
  Overload 1 of 2, '(props: Readonly&lt;{}&gt;): NavigationBar', gave the following error.
    Type '{ title: string; }' is not assignable to type 'IntrinsicAttributes &amp; IntrinsicClassAttributes&lt;NavigationBar&gt; &amp; Readonly&lt;{}&gt; &amp; Readonly&lt;{ children?: ReactNode; }&gt;'.
      Property 'title' does not exist on type 'IntrinsicAttributes &amp; IntrinsicClassAttributes&lt;NavigationBar&gt; &amp; Readonly&lt;{}&gt; &amp; Readonly&lt;{ children?: ReactNode; }&gt;'.
  Overload 2 of 2, '(props: {}, context?: any): NavigationBar', gave the following error.
    Type '{ title: string; }' is not assignable to type 'IntrinsicAttributes &amp; IntrinsicClassAttributes&lt;NavigationBar&gt; &amp; Readonly&lt;{}&gt; &amp; Readonly&lt;{ children?: ReactNode; }&gt;'.
      Property 'title' does not exist on type 'IntrinsicAttributes &amp; IntrinsicClassAttributes&lt;NavigationBar&gt; &amp; Readonly&lt;{}&gt; &amp; Readonly&lt;{ children?: ReactNode; }&gt;'.  TS2769</code></pre>
<p>原因是Typescript是强类型语言，如果要给组件传值，需要在接受值组的地方声明值类型，就本例而言，需要明确定义<code>props</code>,本例为<code>NavigationBarProps</code></p>
<pre><code class="language-ts">class Page extends React.Component
{
    render()
    {
        return &lt;div&gt;
                &lt;NavigationBar title="标题"/&gt;
      &lt;/div&gt;
    }
}

// 定义props类型
interface NavigationBarProps
{
    title:string;
}
class NavigationBar extends React.Component&lt;NavigationBarProps&gt;
{
    render()
    {
        return &lt;div&gt;
            "返回标题"
        &lt;/div&gt;
    }
}</code></pre>
]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>React :  Style prop value must be an object  react/style-prop-object</title>
		<link>https://blog.z6z8.cn/2019/11/11/react-style-prop-value-must-be-an-object-react-style-prop-object/</link>
					<comments>https://blog.z6z8.cn/2019/11/11/react-style-prop-value-must-be-an-object-react-style-prop-object/#respond</comments>
		
		<dc:creator><![CDATA[holdsky]]></dc:creator>
		<pubDate>Mon, 11 Nov 2019 07:57:59 +0000</pubDate>
				<category><![CDATA[React]]></category>
		<category><![CDATA[学习笔记]]></category>
		<guid isPermaLink="false">http://blog.z6z8.cn/?p=558</guid>

					<description><![CDATA[我初学React，懵懵懂懂，我把基础的html语法直接搬到JSX文件中，React提示我: Line 17:3 [&#8230;]]]></description>
										<content:encoded><![CDATA[<p>我初学React，懵懵懂懂，我把基础的html语法直接搬到JSX文件中，React提示我:</p>
<pre><code> Line 17:36:  Style prop value must be an object  react/style-prop-object</code></pre>
<p>我写的是:</p>
<pre><code class="language-jsx">class CYXQHome extends React.Component {
    render(){
        return (
             &lt;div id="showDiv1" style="display:none;position:relative;left:69px;width:360px" /&gt;
             );
      }
}</code></pre>
<p>出现这种错误的原因是<code>style=xxxx</code>那部分，style必须是一个对象 。修改后</p>
<pre><code>&lt;div id="showDiv1" style={ { display:"none",position:"relative",left:"69px",width:"360px" } } /&gt;</code></pre>
<p>如上，有两个花括号, <code> {{ {} }} </code>,外部的<code>{ {} } </code>表示这是Javascript句法，里面的<code>{ {} } </code>是一个对象。</p>
<p>参考 <a href="http://www.mamicode.com/info-detail-1682242.html">http://www.mamicode.com/info-detail-1682242.html</a></p>
]]></content:encoded>
					
					<wfw:commentRss>https://blog.z6z8.cn/2019/11/11/react-style-prop-value-must-be-an-object-react-style-prop-object/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>create-react-app ：不产生source-map来缩减打包文件体积</title>
		<link>https://blog.z6z8.cn/2019/10/15/create-react-app-%ef%bc%9a%e4%b8%8d%e4%ba%a7%e7%94%9fsource-map%e6%9d%a5%e7%bc%a9%e5%87%8f%e6%89%93%e5%8c%85%e6%96%87%e4%bb%b6%e4%bd%93%e7%a7%af/</link>
		
		<dc:creator><![CDATA[holdsky]]></dc:creator>
		<pubDate>Tue, 15 Oct 2019 12:05:08 +0000</pubDate>
				<category><![CDATA[React]]></category>
		<category><![CDATA[学习笔记]]></category>
		<guid isPermaLink="false">http://blog.z6z8.cn/?p=507</guid>

					<description><![CDATA[用create-react-app 脚手架创建的react项目，使用npm run build打包后，默认会产 [&#8230;]]]></description>
										<content:encoded><![CDATA[<p>用create-react-app 脚手架创建的react项目，使用<code>npm run build</code>打包后，默认会产生<code>source-map</code>文件，通常项目上线后这种文件是不必要的。</p>
<p>现在需要配置以关闭这种文件的产生。</p>
<p>打开项目的<code>package.json</code>文件，找到下面类似的配置</p>
<pre><code class="language-javascript">"scripts": {
    "analyze": "source-map-explorer 'build/static/js/*.js'",
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject"
  },</code></pre>
<p>可以看到<code>build</code>命令执行的脚本是<code>react-scripts build</code>,然后打开<br />
<code>node_modules/react-scripts/config/webpack.config.js</code>文件。</p>
<p>一般情况下，定位到这，基本就知道了怎么关闭<code>source-map</code>文件产生--搜索<code>shouldUseSourceMap</code>，将其置为false就行了。</p>
<p>如果仔细查看<code>shouldUseSourceMap</code>定义</p>
<pre><code class="language-javascript">const shouldUseSourceMap = process.env.GENERATE_SOURCEMAP !== 'false';</code></pre>
<p>会发现shouldUseSourceMap和<code>process.env.GENERATE_SOURCEMAP</code>环境变量有关,那么在不修改<code>webpack.config.js</code>文件的情况，其实还可以修改环境变量。( 环境变量修改参考<a href="https://139.155.43.7/2019/10/15/create-react-app-%e8%87%aa%e5%ae%9a%e4%b9%89%e7%8e%af%e5%a2%83%e5%8f%98%e9%87%8f/">https://139.155.43.7/2019/10/15/create-react-app-%e8%87%aa%e5%ae%9a%e4%b9%89%e7%8e%af%e5%a2%83%e5%8f%98%e9%87%8f/</a> )</p>
<p>在项目目录下创建.env文件,添加内容</p>
<pre><code class="language-javascript">GENERATE_SOURCEMAP=false</code></pre>
<p>然后重新执行<code>npm run build</code>，这时应不会产生<code>source-map</code>文件</p>
]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>create-react-app : 自定义环境变量</title>
		<link>https://blog.z6z8.cn/2019/10/15/create-react-app-%e8%87%aa%e5%ae%9a%e4%b9%89%e7%8e%af%e5%a2%83%e5%8f%98%e9%87%8f/</link>
		
		<dc:creator><![CDATA[holdsky]]></dc:creator>
		<pubDate>Tue, 15 Oct 2019 11:45:22 +0000</pubDate>
				<category><![CDATA[React]]></category>
		<category><![CDATA[学习笔记]]></category>
		<guid isPermaLink="false">http://blog.z6z8.cn/?p=503</guid>

					<description><![CDATA[create-react-app是一款非常好用的react项目脚手架，其创建的react项目除了可以访问默认的 [&#8230;]]]></description>
										<content:encoded><![CDATA[<p>create-react-app是一款非常好用的react项目脚手架，其创建的react项目除了可以访问默认的环境变量,还可以自定义环境变量，只要在项目目录下创建<code>.env</code>文件，然后直接写入环境变量就可以了。<br />
具体如下：</p>
<p>1、创建.env文件<br />
在react项目目录下创建<code>.env</code>文件。<br />
<img decoding="async" src="/wp-content/uploads/2019/10/react-env.png" alt="" /></p>
<p>2、写入环境变量，例如</p>
<pre><code class="language-javascript">ABCD=1</code></pre>
<p>3、访问环境变量<br />
对于默认定义好的环境变量，使用<code>process.env.默认环境变量名</code>访问<br />
对于自定义环境变量，需要添加<code>REACT_</code>前缀,即使用<code>process.env.REACT_自定义环境变量名</code>访问</p>
<p>例如</p>
<pre><code class="language-javascript">render() {

if (process.env.REACT_ABCD !== 0 ) {
    '示例'
}
  return (
    &lt;div&gt;
      &lt;span&gt; 默认环境变量示例 {process.env.NODE_ENV &lt;/span&gt;
      &lt;span&gt; 自定义环境变量示例 {process.env.REACT_ABCD &lt;/span&gt;
    &lt;/div&gt;
  );
}</code></pre>
<p>参考 ：<a href="https://create-react-app.dev/docs/adding-custom-environment-variables">https://create-react-app.dev/docs/adding-custom-environment-variables</a></p>
]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>React : 一个简短的路由配置示例</title>
		<link>https://blog.z6z8.cn/2019/10/15/react-%e4%b8%80%e4%b8%aa%e7%ae%80%e7%9f%ad%e7%9a%84%e8%b7%af%e7%94%b1%e9%85%8d%e7%bd%ae%e7%a4%ba%e4%be%8b/</link>
		
		<dc:creator><![CDATA[holdsky]]></dc:creator>
		<pubDate>Tue, 15 Oct 2019 07:48:13 +0000</pubDate>
				<category><![CDATA[React]]></category>
		<category><![CDATA[代码片段]]></category>
		<category><![CDATA[学习笔记]]></category>
		<guid isPermaLink="false">http://blog.z6z8.cn/?p=497</guid>

					<description><![CDATA[为了方便学习React路由，这里记录示例创建过程。 1、创建react app #创建 npx create- [&#8230;]]]></description>
										<content:encoded><![CDATA[<p>为了方便学习React路由，这里记录示例创建过程。</p>
<p><img decoding="async" src="/wp-content/uploads/2019/10/react路由演示.gif" alt="" /></p>
<p>1、创建react app</p>
<pre><code>#创建
npx create-react-app sample
#进入目录
cd sample</code></pre>
<p>2、修改public/index.html</p>
<pre><code class="language-html">&lt;html&gt;
  &lt;head&gt;
    &lt;title&gt;React 路由&lt;/title&gt;
  &lt;/head&gt;
  &lt;body&gt;
    &lt;div id="root"&gt;&lt;/div&gt;
  &lt;/body&gt;
&lt;/html&gt;</code></pre>
<p>3、修改 src/index.js</p>
<pre><code class="language-javascript">import React from 'react';
import ReactDOM from 'react-dom';
import App from './App';

ReactDOM.render(&lt;App /&gt;, document.getElementById('root'));</code></pre>
<p>4、修改src/App.js</p>
<pre><code class="language-javascript">import React from 'react';
import { BrowserRouter as Router,Route,Link} from 'react-router-dom';

function Home () {
  return (
    &lt;div&gt;
      &lt;span&gt;主页&lt;/span&gt;
      &lt;Link to="/page"&gt;其他页面&lt;/Link&gt;
    &lt;/div&gt;
  );
}

function Page () {
  return (
    &lt;div&gt;
      &lt;span&gt;其他页面&lt;/span&gt;
      &lt;Link to="/"&gt;主页&lt;/Link&gt;
    &lt;/div&gt;
  );
}

function App() {
  return (
    &lt;Router&gt;
      &lt;div&gt;
        &lt;Route path="/" exact={true} component={Home} /&gt;
        &lt;Route path="/page" exact={true} component={Page} /&gt;
      &lt;/div&gt;
    &lt;/Router&gt;
  );
}

export default App;</code></pre>
<p>5、安装依赖 react-router-dom</p>
<pre><code>yarn add react-router-dom</code></pre>
<p>6、执行看效果</p>
<pre><code>yarn start</code></pre>
]]></content:encoded>
					
		
		
			</item>
	</channel>
</rss>
